lundi 22 mai 2017

Better practice: Ever looping thread or successive threads?

Currently, my application is making calls to REST APIs periodically, and I'm using one dedicated thread to do that (request() is an object's method):

void MyObject::request() {
    while (m_continue) {
        //API call, printing response, then sleeping 30 secs
        usleep(30*1000);
    }
}

And making a thread like:

thread t(&MyObject::request, this);
t.detach();

But using c++11 or Boost, is it a better practice to use an ever looping thread than multiple successive threads ?

Like this:

string MyObject::request() {
    //API call, printing response
}

And calling it multiple times like the following:

while (m_continue) {
    future<string> f = async(request);
    cout << f.get() << endl;
    usleep(30*1000);
}

Aucun commentaire:

Enregistrer un commentaire