Search code examples
c++c++11asynchronousstdasync

Is std::launch::async policy needed?


What is the difference in the following:

std::async(my_function);

and

std::async(std::launch::async, my_function);

What is the difference in using the pilicy std::launch::async in this case?? Does the first option not launch the function asynchronously anyway??


Solution

  • The first one is equivalent to passing launch::async | launch::deferred, in which case it is up to the implementation whether it is launched asynchronously or merely deferred (called when a non-timed waiting function like get() is called on the returned future).

    The idea is that by default, the implementation can choose to defer if creating a new thread isn't going to be a performance gain. To force a new thread to be created, pass launch::async only.