Does standard C++11 guarantee that std::async(std::launch::async, func) launches func in separate thread?

随声附和 提交于 2019-12-03 12:35:59

The two key statements here are:

as if in a new thread of execution represented by a thread object

The 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!