async, future & atomic in C++

From the C++ cheat sheet · Concurrency · verified Jul 2026

async, future & atomic

Results from background work and lock-free counters.

cpp
// Quick Reference
#include <future>
auto fut = std::async([]{ return 40 + 2; });
int result = fut.get();          // 42

#include <atomic>
std::atomic<int> count{0};
count++;                         // atomic, no mutex
💡 std::async returns a future; call .get() once to retrieve the value (it blocks until ready).
⚡ std::atomic gives race-free single-variable updates without a mutex.
📌 Pass std::launch::async to force a new thread; the default may run lazily on get().
🟢 future.get() rethrows any exception the async task threw - handle it at the call site.
asyncatomic

More C++ tasks

Back to the full C++ cheat sheet