std::async (3) - Linux Manuals
std::async: std::async
NAME
Synopsis
Defined in header <future>
template< class Function, class... Args> (since C++11)
std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> (until C++17)
async( Function&& f, Args&&... args );
template< class Function, class... Args>
std::future<std::invoke_result_t<std::decay_t<Function>, (since C++17)
std::decay_t<Args>...>> (until C++20)
async( Function&& f, Args&&... args );
template< class Function, class... Args>
[[nodiscard]]
std::future<std::invoke_result_t<std::decay_t<Function>, (since C++20)
std::decay_t<Args>...>>
async( Function&& f, Args&&... args ); (1)
template< class Function, class... Args > (since C++11)
std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>> (until C++17)
async( std::launch policy, Function&& f, Args&&... args );
template< class Function, class... Args >
std::future<std::invoke_result_t<std::decay_t<Function>, (since C++17)
std::decay_t<Args>...>> (2) (until C++20)
async( std::launch policy, Function&& f, Args&&... args );
template< class Function, class... Args >
[[nodiscard]]
std::future<std::invoke_result_t<std::decay_t<Function>, (since C++20)
std::decay_t<Args>...>>
async( std::launch policy, Function&& f, Args&&... args );
The template function async runs the function f asynchronously (potentially in a separate thread which may be 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. In other words, f may be executed in another thread or it may be run synchronously when the resulting std::future is queried for a value.
2) Calls a function f with arguments args according to a specific launch policy policy:
In any case, the call to std::async synchronizes-with (as defined in std::memory_order) the call to f, and the completion of f is sequenced-before making the shared state ready. If the async policy is chosen, 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.
Parameters
f - Callable object to call
args... - parameters to pass to f
policy - std::launch::async enable asynchronous evaluation
Type requirements
-
Function, Args must meet the requirements of MoveConstructible.
Return value
std::future referring to the shared state created by this call to std::async.
Exceptions
Throws std::system_error with error condition std::errc::resource_unavailable_try_again if the launch policy equals std::launch::async and the implementation is unable to start a new thread (if the policy is async|deferred or has additional bits set, it will fall back to deferred or the implementation-defined policies in this case), or std::bad_alloc if memory for the internal data structures could not be allocated.
Notes
The implementation may extend the behavior of the first overload of std::async by enabling additional (implementation-defined) bits in the default launch policy.
Examples of implementation-defined launch policies are the sync policy (execute immediately, within the async call) and the task policy (similar to async, but thread-locals are not cleared)
If the std::future obtained from std::async is not moved from or bound to a reference, the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes, essentially making code such as the following synchronous:
(note that the destructors of std::futures obtained by means other than a call to std::async never block)
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2021 C++11 return type incorrect and value category of arguments unclear in the deferred case corrected return type and clarified that rvalues are used
Example
// Run this code