Does standard C++11 guarantee that std::async(std::launch::async, func)
launches function in separate thread?
Working Draft, Standard for Programming Langua
The two key statements here are:
as if in a new thread of execution represented by a
thread
objectThe
thread
object is stored in the shared state and affects the behavior of any asynchronous return objects that reference that state.
"As if" means it must behave exactly as if it had created a std::thread
object for this function. Which means that all side effects of the creation of a std::thread
must also happen.
That being said, if you combine launch::async
with launch::deferred
, then the implementation decides whether to launch a new thread or defer it to an existing one. So it's only launch::async
alone that requires a new thread.