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

前端 未结 1 2082
余生分开走
余生分开走 2021-02-14 08:31

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

Working Draft, Standard for Programming Langua

相关标签:
1条回答
  • 2021-02-14 08:38

    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.

    0 讨论(0)
提交回复
热议问题