Namespaces
Variants
Actions

std::async

From cppreference.com
< cpp‎ | thread
 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
async
(C++11)
(C++11)
Safe reclamation
(C++26)
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
(C++11)(deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
 
Defined in header <future>
template< class F, class... Args >
std::future</* see below */> async( F&& f, Args&&... args );
(1) (since C++11)
template< class F, class... Args >

std::future</* see below */> async( std::launch policy,

                                    F&& f, Args&&... args );
(2) (since C++11)

The function template std::async runs the function f asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std::future that will eventually hold the result of that function call.

1) Behaves as if (2) is called with policy being std::launch::async | std::launch::deferred.
2) Calls a function f with arguments args according to a specific launch policy policy (see below).

The return type of std::async is std::future<V>, where V is:

typename std::result_of<typename std::decay<F>::type(
                        typename std::decay<Args>::type...)>::type.

(until C++17)

std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>.

(since C++17)


If any of the following conditions is satisfied, the program is ill-formed:

(until C++20)

If any of the following is false, the program is ill-formed:

(since C++20)

The call to std::async synchronizes with the call to f, and the completion of f is sequenced before making the shared state ready.

Contents