Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Working Draft, Standard for Programming Language C++ 2016-07-12: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

1. On the one hand, C++11-Standard says that if the thread can not be created, then there is an error. This ensures the creation of a new thread (in the absence of errors).

§ 30.6.8

6

Throws: system_error if policy == launch::async and the implementation is unable to start a new thread.

7 Error conditions:

(7.1) — resource_unavailable_try_again — if policy == launch::async and the system is unable to start a new thread.

And documentation says: http://en.cppreference.com/w/cpp/thread/launch

std::launch::async a new thread is launched to execute the task asynchronously

2. On the other hand, it is written that the thread can be potentially created. Those, is not necessary that thread should be created.

§ 30.6.8

1 The function template async provides a mechanism to launch a function potentially in a new thread and provides the result of the function in a future object with which it shares a shared state.

And here written as if in a new thread, does it mean not necessary in new separate thread?

§ 30.6.8

(3.1)

— if policy & launch::async is non-zero — calls INVOKE (DECAY_COPY (std::forward(f)), DECAY_COPY (std::forward(args))...) (20.14.2, 30.3.1.2) as if in a new thread of execution represented by a thread object with the calls to DECAY_COPY () being evaluated in the thread that called async. Any return value is stored as the result in the shared state. Any exception propagated from the execution of INVOKE (DECAY_COPY (std::forward(f)), DECAY_COPY (std::forward(args))...) is stored as the exceptional result in the shared state. The thread object is stored in the shared state and affects the behavior of any asynchronous return objects that reference that state.

When used std::async(std::launch::async, func) then does standard C++11 guarantee that func() will be executed in separate thread, or it can be executed in the same thread that called async?

like image 712
Alex Avatar asked Feb 02 '17 14:02

Alex


People also ask

Does C++ have async await?

Async and await in C++ helps in writing asynchronous code simply. Calculation and getting data from I/O is an example for Async/await operations.

Does STD async start a new thread?

If the async flag is set, then a callable function will be executed in a separate thread. If the deferred flag is set, a callable function will be stored together with its arguments, but the std::async function will not launch a new thread.

How is C++ async implemented?

You can implement your own explicit async with a promise and a thread. std::async is not guaranteed to be parallel if an std::launch policy of std::launch::async is not used. An implementation is also free to use other implementation defined values as the default and can even execute the callback eagerly.


1 Answers

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.

like image 69
Nicol Bolas Avatar answered Nov 04 '22 12:11

Nicol Bolas