vendredi 29 mai 2015

Is std::async guaranteed to be called for functions returning void?

I've wrote the following code to test std::async() on functions returning void with GCC 4.8.2 on Ubuntu.

#include <future>
#include <iostream>

void functionTBC()
{
    std::cerr << "Print here\n";
}

int main(void)
{
#ifdef USE_ASYNC
    auto i = std::async(std::launch::async, functionTBC);
#else
    auto i = std::async(std::launch::deferred, functionTBC);
#endif
    //i.get();
    return 0;
}

If i.get(); is uncommented, the message "Print here" always exists; however, if i.get(); is commented out, "Print here" exists if and only if USE_ASYNC is defined (that is, std::launch::async always leads to message printed out while std::launch::deferred never).

Is this guaranteed behavior? What's the correct way to ensure the asynchronous call returning void to be executed?

Aucun commentaire:

Enregistrer un commentaire