std::future / std::promise / std::async — what are the launch policies?
C++Level: IntermediateConcurrency & OS
std::future / std::promise / std::async — what are the launch policies?
Hints
- Separate concurrency from parallelism and identify shared state.
- Consider locking, access ordering and I/O-bound versus CPU-bound work.
Answer
C++ Futures and Async
:Source: src/thread/future-basic
C++11 introduced std::future and std::async as higher-level abstractions
for asynchronous programming. A future represents a value that will be available
at some point, allowing you to launch computations and retrieve results later
without manually managing threads. The get() method blocks until the result
is ready, while wait_for() allows polling with a timeout to check if the
computation has completed.
Unlike raw threads where you must handle synchronization manually, futures
automatically propagate exceptions from the async task to the calling thread
when you call get(). This makes error handling cleaner and prevents silent
failures in concurrent code.
#include <future>
#include <thread>
int Compute(int x) { return x * x; }
int main() {
std::future<int> fut = std::async(std::launch::async, Compute, 5);
int result = fut.get(); // blocks until result ready, returns 25
// check if ready without blocking
std::future<int> fut2 = std::async(std::launch::async, [] {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return 42;
});
auto status = fut2.wait_for(std::chrono::milliseconds(100));
if (status == std::future_status::ready) {
int val = fut2.get(); // 42
}
}
Launch Policies
:Source: src/thread/future-async
The std::async function accepts a launch policy that controls when and how
the task executes. std::launch::async guarantees the task runs on a new
thread immediately, while std::launch::deferred delays execution until
get() or wait() is called, running the task on the calling thread.
The default policy (async | deferred) lets the implementation choose,
which can lead to surprising behavior if you expect parallel execution.
For CPU-bound parallel algorithms, use std::launch::async explicitly to
ensure tasks run concurrently. Deferred execution is useful for lazy evaluation
where the result might not be needed, avoiding the overhead of thread creation.
#include <future>
#include <numeric>
#include <vector>
int main() {
// async: runs immediately on new thread
auto fut1 = std::async(std::launch::async, [] { return 100; });
// deferred: runs lazily when get() called
auto fut2 = std::async(std::launch::deferred, [] { return 200; });
// parallel sum example
std::vector<int> v(1000);
std::iota(v.begin(), v.end(), 1); // 1 to 1000
auto mid = v.begin() + v.size() / 2;
auto sum1 = std::async(std::launch::async, [&] {
return std::accumulate(v.begin(), mid, 0);
});
auto sum2 = std::async(std::launch::async, [&] {
return std::accumulate(mid, v.end(), 0);
});
int total = sum1.get() + sum2.get(); // 500500
}
Source and licence: C/C++ Interview Cheatsheet — Chang Ning Tsai · MIT