cpp/thread/async

The function template runs the function  asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std that will eventually hold the result of that function call. @1@ Behaves as if is called with  being. @2@ Calls a function with arguments  according to a specific launch policy  (see below).

The call to synchronizes-with (as defined in std) the call to, and the completion of  is sequenced-before making the shared state ready.

Return value
std referring to the shared state created by this call to.

Async invocation
If the async flag is set (i.e. ), then

in the current thread. If the function returns a value or throws an exception, it is stored in the shared state accessible through the std that  returns to the caller.

Deferred invocation
If the deferred flag is set (i.e. ), then stores  in the shared state.

Lazy evaluation is performed:
 * The first call to a non-timed wait function on the std that returned to the caller will evaluate  in the current thread (which does not have to be the thread that originally called ), where
 * is the stored value of and
 * is the stored copy of.


 * The result or exception is placed in the shared state associated with the returned std and only then it is made ready. All further accesses to the same std will return the result immediately.

Other policies
If neither std nor std, nor any implementation-defined policy flag is set in, the behavior is undefined.

Policy selection
If more than one flag is set, it is implementation-defined which policy is selected. For the default (both the std and std flags are set in ), standard recommends (but does not require) utilizing available concurrency, and deferring any additional tasks.

If the std policy is chosen,
 * a call to a waiting function on an asynchronous return object that shares the shared state created by this call blocks until the associated thread has completed, as if joined, or else time out; and
 * the associated thread completion synchronizes-with the successful return from the first function that is waiting on the shared state, or with the return of the last function that releases the shared state, whichever comes first.

Exceptions
Throws
 * std, if the memory for the internal data structures cannot be allocated, or
 * std with error condition std, if and the implementation is unable to start a new thread.
 * If is  or has additional bits set, it will fall back to deferred invocation or the implementation-defined policies in this case.