What model does C++26's std::execution introduce — senders, receivers and schedulers — and what problems does it address?
answer
- lazy description of async work
- three completion channels
- connect, then start
- schedulers say where
- stop is a first-class outcome
basics
~20 sstd::execution describes asynchronous work as senders: lazy objects that, once connected to a receiver and started, complete through set_value, set_error or set_stopped. Schedulers say where work runs; then, let_value and when_all compose senders; sync_wait blocks for the result.
solid answer
~40 sA **sender** describes work without running it. Connecting it to a **receiver** with `connect` yields an **operation state**; `start` launches it, and the work completes by calling exactly one of the receiver's channels: **`set_value`**, **`set_error`** or **`set_stopped`**. A **scheduler** represents an execution resource — a thread pool, an event loop — and `schedule(sch)` returns a sender that completes there. Algorithms compose senders: `then` and `let_value` chain steps, `when_all` joins them, `starts_on` and `continues_on` move work between schedulers, `bulk` fans out. `std::this_thread::sync_wait` blocks for the result, and `spawn` starts work inside a scope such as `counting_scope`. Compared with `std::async` and `std::future`, it is lazy, composable without blocking, allocation-aware, cancellable through stop tokens, and works with coroutines through `std::execution::task`.
go deeper
Know that C++26 adds std::execution for describing asynchronous work, and that it replaces ad hoc callbacks and futures with composable pieces.
Explain sender, receiver, scheduler and the three completion channels, and read a then and let_value pipeline ending in sync_wait.
Compare it with std::async and futures — laziness, allocation, cancellation, structured lifetimes — and say how you would adopt it while library support is missing.
Decide whether the team's async layer converges on this model, how existing executors and coroutine task types bridge to it, and when to switch from a separate implementation to the standard library's.
## Why a new model Before C++26, the standard's tools for asynchronous work were `std::async`, `std::future` and `std::promise`. They are hard to compose: a future can only be waited on or polled, continuations need a thread to block, there is no standard way to cancel, and the shared state usually allocates. Real systems built their own executors, callback chains and task types. **`std::execution`**, from the `<execution>` header, gives C++26 a common vocabulary so that libraries for networking, GPUs, thread pools and coroutines can interoperate. ## The core concepts | Concept | Role | |---|---| | **scheduler** | a handle to an execution resource; `schedule(sch)` returns a sender that completes on it | | **sender** | a lazy description of work and of the values it will produce | | **receiver** | the continuation: three completion functions plus an environment | | **operation state** | the result of `connect(sender, receiver)`; `start(op)` runs it | A sender completes by calling exactly one **completion channel** on its receiver: - `set_value(rcvr, values...)` — success, with zero or more values; - `set_error(rcvr, err)` — failure, often an `std::exception_ptr` or an error code; - `set_stopped(rcvr)` — cancellation, treated as an outcome of its own rather than an error. Nothing runs until `start`, which is what makes senders **lazy**: a pipeline can be built, passed around and adapted before any work is scheduled, and the operation state can live on the stack instead of in a heap-allocated shared state. ## Composing work Sender **factories** create work (`just(values...)`, `just_error`, `just_stopped`, `schedule`), **adaptors** transform it (`then`, `upon_error`, `let_value`, `when_all`, `starts_on`, `continues_on`, `bulk`), and **consumers** run it (`std::this_thread::sync_wait`, `spawn`): ```cpp // C++26 (N5050): not compiled here; neither libc++ nor libstdc++ 16 ships senders namespace ex = std::execution; ex::scheduler auto pool = ex::get_parallel_scheduler(); ex::sender auto work = ex::schedule(pool) | ex::then([] { return load_config(); }) | ex::let_value([](Config& c) { return fetch_all(c); }) // next step is itself a sender | ex::then([](Report r) { return summarise(r); }); auto [summary] = std::this_thread::sync_wait(std::move(work)).value(); ``` `sync_wait` connects and starts the sender, drives a `run_loop` on the calling thread until it completes, rethrows an error, and returns the values in a `std::optional` of a tuple — empty if the work was stopped. ## Cancellation and structured concurrency Stop requests flow through the receiver's environment as stop tokens, and a stopped operation completes on the `set_stopped` channel, so every adaptor knows how to propagate cancellation. For work whose lifetime is not tied to a single `sync_wait`, C++26 adds **async scopes**: `spawn` eagerly starts a sender associated with a scope such as `counting_scope`, and the scope can be joined so that nothing outlives the objects it uses. That is **structured concurrency**: child work finishes before its parent's resources go away. ## Coroutines and senders C++26 also adds `std::execution::task<T>`, a coroutine type whose body can `co_await` senders, and `as_awaitable` to adapt a sender for other coroutine types. Coroutines then become the readable way to write sequential async logic, while senders remain the interoperable plumbing underneath. ## Compared with std::async and futures | | `std::async` and `std::future` | `std::execution` senders | |---|---|---| | When work starts | immediately, or deferred until `get` | only at `start` | | Composition | block on `get`, then continue | `then`, `let_value`, `when_all` without blocking | | Cancellation | none standard | stop tokens and the `set_stopped` channel | | Where work runs | implementation-chosen thread | an explicit scheduler | | Allocation | shared state, usually on the heap | operation state, often on the stack | The practical consequences for a codebase: - library authors can expose **one sender-based interface** that callers consume with `sync_wait`, coroutines or further adaptors; - **thread hops** become explicit, with `continues_on(sch)` naming where the next step runs; - **error, value and cancellation paths** are separated by type, instead of being folded into exceptions. ## Status in 2026 The model is the most library-heavy of C++26's headline features. The libc++ status page for LLVM 23 lists `std::execution` as not implemented, and libstdc++ 16's `<execution>` still contains only the C++17 parallel-algorithm policies, so teams that want the model today use a separate implementation of it. In an interview, explain the model — lazy senders, three channels, schedulers, structured lifetimes — and be explicit that standard library support is still arriving.
- Why does std::execution treat stopped as a separate channel instead of an error?Cancellation is a normal outcome — a user closed the page, a timeout fired, a sibling task failed — not a failure of the operation. A distinct `set_stopped` channel lets adaptors propagate it without inventing an error value, lets `when_all` cancel the remaining children cleanly, and lets `sync_wait` report it as an empty `std::optional` rather than an exception.
- How does a sender pipeline avoid the allocation that std::future usually needs?A future and its promise share state that must outlive both, so implementations normally allocate it. A sender is connected to a known receiver, and `connect` returns an operation state whose type is fully known at compile time; the caller can keep it on the stack or inside another operation state, so a whole pipeline often needs no allocation.
- A sender starts running as soon as it is created, like std::async.
- Cancellation in std::execution is reported as an exception.
- Senders are just C++26's new name for std::future.
- A scheduler is a thread that the sender owns.
- std::this_thread::sync_wait is the only way to consume a sender.
Assumes the C++26 final draft N5050 (WG21 finished, ISO publication pending); C++23 is the latest published standard, so support varies by toolchain.