skip to content

Futures and Async

std::async returns a future whose destructor may block, promise and packaged_task connect a result to it, and the launch policy decides where work runs. Interviewers ask about all three.

on this pageshow

explore

questions

5

In C++, what does std::async do when called without a launch policy, and why can a wait_for polling loop then spin forever?

level: middleimportance: must knowfreq 66%

answer

  1. two bits, implementation picks
  2. deferred means lazy, same thread
  3. future_status::deferred is never ready
  4. say launch::async when you need it

basics

~20 s

Without a policy std::async uses launch::async | launch::deferred and the implementation chooses. A deferred task runs only when get() or wait() is called, in the caller's thread, and wait_for returns future_status::deferred, never ready, so a polling loop never ends.

solid answer

~40 s

The one-argument form behaves as `std::async(std::launch::async | std::launch::deferred, f, args...)`, and the standard lets the implementation pick either. Under `launch::async` the function runs as if on a new `std::thread`. Under `launch::deferred` nothing runs until the first non-timed wait — `get()` or `wait()` — which runs it **in that calling thread**; `wait_for` and `wait_until` do not run it and return `future_status::deferred`. A loop such as `while (f.wait_for(10ms) != std::future_status::ready)` therefore spins forever if the task was deferred. libc++ and libstdc++ both start a thread whenever they can and defer only if thread creation fails, but portable code must not depend on that: pass `std::launch::async` when concurrency is required, or check for `future_status::deferred`.

code

cpp · 23 lines
cpp
#include <chrono>
#include <future>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;

int main() {
    auto caller = std::this_thread::get_id();
    auto where = [caller] { return std::this_thread::get_id() == caller ? "caller" : "other"; };

    auto eager = std::async(std::launch::async, where);
    auto lazy  = std::async(std::launch::deferred, where);

    std::cout << std::boolalpha
              << "lazy wait_for says deferred: "
              << (lazy.wait_for(0s) == std::future_status::deferred) << '\n'
              << "eager ran on: " << eager.get() << " thread\n"
              << "lazy ran on:  " << lazy.get() << " thread\n";

    auto never = std::async(std::launch::deferred, where);
    // while (never.wait_for(10ms) != std::future_status::ready) {}  // would spin forever
    std::cout << "never.valid(): " << never.valid() << '\n';
}   // never's deferred function is simply not called

go deeper

for a junior

Know the two policies, launch::async and launch::deferred, and that the default lets the library pick between them.

for a middle

Explain where and when a deferred task runs, why wait_for returns future_status::deferred, and why the polling loop spins.

for a senior

Write policy-explicit std::async calls, check for deferred where timed waits are used, and spot caller-held locks and thread_local state that a deferred task would share.

for a principal

Decide whether the codebase permits the default policy at all; requiring an explicit policy is a cheap rule that removes a class of portability bugs.

## The two policies `std::launch` is a bitmask type with two standard enumerators: - **`std::launch::async`** — the function is called "as if in a new thread of execution represented by a `thread` object". Its result or exception goes into the shared state, and waiting on the future blocks until that thread has completed, as if joined. - **`std::launch::deferred`** — the function and copies of its arguments are stored in the shared state as a **deferred function**. It is not run until the first call to a **non-timed waiting function** — `get()` or `wait()` — and then it runs in the thread that made that call. Passing a policy with no bits set, or with bits the standard and implementation do not define, is undefined behaviour. ## The default The overload without a policy is specified as calling the other with `launch::async | launch::deferred`. When both bits are set, "the implementation may choose any of the corresponding policies". The standard adds a recommendation that implementations defer "when no more concurrency can be effectively exploited", and its own example notes that a default-policy `std::async` "might not result in concurrency" because the lambda might not run until `get()`. ## What two implementations actually do | Library | Default policy behaviour (read from the headers) | |---|---| | libc++ (LLVM 23) | tries to start a detached `std::thread`; if that throws and the policy was not `launch::async` alone, falls back to deferred | | libstdc++ (GCC 16) | starts a thread; falls back to deferred only when thread creation fails with `resource_unavailable_try_again` and the deferred bit is set | So on these libraries the default nearly always runs concurrently, which is exactly why code that silently depends on it passes tests and then misbehaves elsewhere or under load. ## The `wait_for` trap ```cpp auto f = std::async(work); // default policy while (f.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready) { update_progress_bar(); // never ends if work was deferred } ``` For a deferred task, `wait_for` has no effect except returning `future_status::deferred`. It never runs the function and never returns `ready`, so the loop spins forever. The robust forms are: 1. Pass `std::launch::async` explicitly when the caller needs the work to proceed while it does something else. 2. Or test for the deferred case first: `if (f.wait_for(0s) == std::future_status::deferred) { /* run it now with get() */ }`. ## Other consequences of a deferred task - **It runs in the caller's thread**, so its `thread_local` variables are the caller's, and a lock the caller holds while calling `get()` is still held during the task; if the task locks the same `std::mutex`, the thread locks a mutex it already owns, which is undefined behaviour and in practice usually a self-deadlock. - **It may never run.** If nobody calls `get()` or `wait()`, the function is not called at all; the standard notes that "it is possible for `f` not to be called at all". - **No parallelism** between the caller's work and the task, as in the standard's own example. - **Arguments are copied at the `std::async` call** under both policies, so a deferred task sees the values at launch time, not at `get()` time. ## How arguments reach the task Under both policies `std::async` copies its arguments at the call: the standard stores `auto(std::forward<Args>(args))...`, a decayed copy of each. That has three consequences. - A **deferred** task sees the values as they were at the `std::async` call, not at `get()`. - To pass a reference, wrap it in `std::ref` or `std::cref`; the task then reads the referenced object when it runs, and that object must still be alive — under `launch::deferred` possibly long after the call. - A **move-only** argument such as a `std::unique_ptr` is moved into the shared state and then moved into the call, so `std::async(std::launch::async, consume, std::move(buffer))` works. The same decay-copy rule applies to the callable itself, so a lambda capturing by reference carries the same lifetime obligations as `std::ref`. ## Choosing a policy | Intent | Policy | |---|---| | The work must overlap with the caller | `std::launch::async` | | Lazy evaluation; run only if the result is needed | `std::launch::deferred` | | Either is acceptable and you will only ever call `get()` | the default | Spelling the policy out also documents the intent for the next reader, which is often worth more than the few characters saved.

  • Does a deferred std::async task ever run if nobody calls get() or wait()?
    No. A deferred function runs only on the first call to a non-timed waiting function on its future. If the future is destroyed without that, the function is never called, and its side effects never happen. `wait_for` and `wait_until` do not count: they return `future_status::deferred` without running it.
  • What happens if the caller holds a std::mutex across get() and the task locks the same mutex?
    Under `launch::deferred` the task runs inside `get()` on the caller's thread, so it locks a mutex that thread already owns: undefined behaviour, usually a self-deadlock. Under `launch::async` the task runs elsewhere and blocks on the mutex while the caller waits in `get()` for the task: an ordinary deadlock. Either way the rule is the same: never hold a lock the task needs across `get()`.
  • When is launch::deferred the right choice?
    When the result may not be needed and computing it should cost nothing unless someone asks, for example an expensive diagnostic string built only if an error report is printed. It gives lazy evaluation with the same `std::future` interface, at the price of no concurrency.

  • The default policy always starts a new thread
  • wait_for runs a deferred task so that it can report ready
  • A deferred task runs on a library background thread
  • launch::deferred evaluates the arguments when get() is called
  • Every deferred task eventually runs, even if its future is never waited on
open as a page

A C++ thumbnail generator calls std::async(std::launch::async, make_thumbnail, img) in a loop, keeping each future only for that iteration; why does it run sequentially?

level: seniorimportance: must knowfreq 55%

basics

~20 s

Each iteration's std::future dies at the end of the loop body, and the last future of an unfinished std::async task may block in its destructor until the task ends, as libc++ and libstdc++ do. Keep the futures and get() them after the loop.

open as a page

In C++, how does std::future::get deliver a std::async task's result or exception, and why can it be called only once?

level: juniorimportance: should knowfreq 58%

basics

~20 s

std::future::get waits until the shared state is ready, then moves the stored value out or rethrows the stored exception. It releases the state, so valid() becomes false and a second get() is undefined; std::shared_future allows repeated get().

open as a page

In C++, how do std::promise and std::packaged_task each connect a result to a std::future, and how does an exception reach the caller?

level: middleimportance: should knowfreq 45%

basics

~20 s

A std::promise is set by hand with set_value or set_exception; a std::packaged_task wraps a callable and stores its return value or exception when called. get() rethrows a stored exception; a provider destroyed unset stores broken_promise.

open as a page

Your C++ codebase launches background work with std::async everywhere; would you ban it in favour of a thread pool, and what would you standardise instead?

level: principalimportance: should knowfreq 30%

basics

~20 s

Usually restrict rather than ban: std::async cannot cap thread count, may defer by default, starts a thread per call in libc++ and libstdc++, and hides waits in destructors. Route frequent work through one owned pool; keep launch::async for rare coarse tasks.

open as a page