CKTIS / C++
std::async: a task associated with a future
std::async starts a callable according to a std::launch policy and returns a future holding either its result or its exception.
Version: C++23
Syntax
template<class F, class... Args>
std::future<std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
async(std::launch policy, F&& function, Args&&... arguments);
template<class F, class... Args>
auto async(F&& function, Args&&... arguments);std::async from <future> accepts a callable and arguments, exposing the outcome through std::future. std::launch::async requires separate execution, deferred postpones invocation until get() or wait(), and the default combination lets the implementation choose.
Arguments are copied or moved into the task by default; use std::ref only when a reference is intentional and its lifetime is guaranteed. The return value or exception enters the shared state, and future::get() retrieves it only once.
Discarding a temporary future from an async task may block in its destructor, accidentally serializing calls. Do not assume concurrency without an explicit policy. Captured objects must outlive their use, and shared data still requires synchronization. Likewise, every object captured by reference in the callable or lambda must remain alive until its last use during task execution.
Examples
#include <future>
#include <iostream>
int main() {
auto result = std::async(std::launch::async, [] { return 3 + 4; });
std::cout << result.get() << '\n';
}
Result
7
std::async → 7