std::async
std::async runs a function asynchronously and gives you a std::future for the result (or the exception). The launch policy chooses a new thread or a deferred call that runs when you get.
Detailed Explanation
You want parse(file) to run while you keep serving requests, and you want the parsed tree or the error when you are ready. auto fut = std::async(std::launch::async, parse, path); starts parse on another thread. fut.get() waits and returns the value, or rethrows what parse threw. That is the whole product: thread plus exception-safe result channel.
Launch policies:
std::launch::async— must run on its own thread as ifstd::thread.std::launch::deferred— run in the caller on the firstwait/get(lazy).async | deferred— the implementation chooses. This is the default if you omit the policy.
The default is the trap. A deferred task does not run until get. If you never get, it never runs. If you fire-and-forget std::async(f) and ignore the future, the future's destructor waits for an async task (and runs a deferred one). A temporary std::async(f); therefore blocks the current thread at the semicolon. Store the future, or do not use async for fire-and-forget (use jthread).
std::launch::async is the policy you should write when you mean "now, on another thread".
The future from async is special: its destructor joins the task if the task was launched async. Other futures (from promise) do not join a thread because they are not a thread.
async is not a thread pool. Each launch::async may create an OS thread. A thousand async calls can exhaust the machine. For bulk work, use a pool (or C++17 parallel algorithms, or an executor library).
Arguments are decay-copied like std::thread. Use std::ref for references you will keep alive until get. Do not pass pointers to locals and then let the async outlive the locals without getting first.
std::async is fine for a handful of independent tasks whose results you wait for at a join point. It is the wrong primitive for a server's per-request concurrency (use a pool) and for a GUI thread (blocking get freezes the UI).
Code Examples
#include <future>
#include <iostream>
#include <string>
int parse(const std::string& s) { return static_cast<int>(s.size()); }
int main() {
auto fut = std::async(std::launch::async, parse, std::string{"abcd"});
std::cout << fut.get() << '\n'; // prints: 4
}#include <future>
#include <stdexcept>
int fail() { throw std::runtime_error("boom"); }
int main() {
auto fut = std::async(std::launch::async, fail);
try {
fut.get();
} catch (const std::runtime_error&) {
return 0;
}
return 1;
}Common Gotchas
Discarding the future
`std::async(std::launch::async, f);` blocks at the end of the statement because the temporary future's destructor waits. Store the future.
Default policy may be deferred
the work might run on `get` in the calling thread. Write `std::launch::async` when you need parallelism.
Not a pool
spawning a thread per small task is slower than doing the work.
`get` rethrows
handle exceptions at the `get` site, not only inside the task.