Execution control library
A guide to chapter 33, std::execution: a sender describes work, connect binds it to a receiver to form an operation state, and only start actually runs it.
Key points
std::execution(C++26, header<execution>) is one set of interfaces for describing where a piece of work runs and who receives the result when it finishes. It applies to thread pools, event loops, GPUs and coroutines.- A sender is only a description of work;
connectcombines it with a receiver into an operation state, and the work begins only afterstart. - Every started operation completes exactly once, with exactly one of
set_value,set_errororset_stopped. - All customization goes through member functions:
rcvr.set_value(...),sndr.connect(r),env.query(q). Completion functions,startand queries must all benoexcept. - Clang 23's libc++ doesn't have the sender part yet, so none of the examples in this chapter were compiled in our tests.
Why a new model#
std::async plus std::future can move work onto another thread, but there are things it can't do: a future always allocates a shared state on the heap; async starts running as soon as it's called, so you can't describe first and start later; there is no cancellation; and the standard has no then-style chaining.
Chapter 33 answers these with four concepts C++26, mostly from P2300R10:
| Role | Concept | Purpose |
|---|---|---|
| Sender | sender |
Describes a piece of asynchronous work; executes nothing itself |
| Receiver | receiver |
The continuation after the work completes, with three entry points: set_value, set_error, set_stopped |
| Operation state | operation_state |
The result of connect(sndr, rcvr), holding everything this run needs; start(op) begins execution |
| Scheduler | scheduler |
A handle to an execution resource; schedule(sch) gives a sender that moves onto that resource |
A complete lifecycle#
About the examples in this chapter
In Clang 23's libc++, <execution> has only the execution policies, not sender facilities such as std::execution::just, and no reference implementation was installed in our tests. The examples in this chapter are written against the draft, have not been compiled, and carry no verdict.
#include <execution>
#include <print>
#include <utility>
namespace ex = std::execution;
int main() {
ex::parallel_scheduler sch = ex::get_parallel_scheduler();
ex::sender auto work = ex::schedule(sch) // hop to the thread pool
| ex::then([] { return 6; }) // compute 6
| ex::then([](int x) { return x * 7; }); // multiply by 7
// sync_wait connects and starts internally, blocking the current thread until completion
auto [v] = std::this_thread::sync_wait(std::move(work)).value();
std::println("{}", v); // 42
}What sync_wait does internally ([exec.sync.wait]) is the skeleton of the whole model:
- Build the sender.
schedule(sch) | then(f) | then(g)just packssch,fandginto a nested object. [exec.adapt.general]/3 requires that, beforeconnectandstart, an adaptor has no observable effects other than copying and moving its arguments. - connect.
connect(work, rcvr)connects each sender layer to the receiver of the next layer, producing an operation state object. It usually lives right on the caller's stack and nests the states of all child operations, so no heap allocation is needed. - start.
start(op)starts the outermost operation, which starts its children. A thread-pool thread executesf, the result 6 is handed viaset_valueto the receiver ofthen(g), and after computing 42 it is handed tosync_wait's receiver. - Completion. Each operation completes exactly once ([exec.async.ops]/8). After completion the operation state is done with, and only then does
sync_waitlet it go out of scope.
The terms these steps use (asynchronous operation, operation state, asynchronous lifetime and so on) are all defined in [exec.async.ops], and every later subclause builds on it.
Map of the chapter#
| Topic | Pages | Contents |
|---|---|---|
| Basic model | [exec.async.ops] | Definitions of asynchronous operation, operation state, receiver, sender, scheduler, completion signature |
| Queries and environments | [exec.queryable], [exec.queries] | Queryable objects, prop, env; queries such as get_stop_token and get_scheduler |
| Three core concepts | [exec.sched], [exec.recv], [exec.opstate] | scheduler, receiver, operation_state, plus set_* and start |
| Senders | [exec.snd] | The sender and sender_in concepts, the exposition-only skeleton of standard senders |
| Completion signatures | [exec.cmplsig] | completion_signatures, get_completion_signatures, dependent senders |
| Connecting and customizing | [exec.connect], [exec.snd.transform] | connect, treating awaitables as senders; domains and transform_sender |
| Factories | [exec.factories] | schedule, just, read_env |
| Adaptors | <a class="xref std local" href="../../en/std/exec.adapt.html" data-tip="33.9.12 Sender adaptors — The general rules for adaptors (laziness, how attributes and environments are forwarded, the exception_ptr signature that may be omitted); the closure objects behind sndr | then(f); then, upon_error, upon_stopped; write_env and unstoppable; into_variant; stopped_as_optional and stopped_as_error.">[exec.adapt], [exec.on], [exec.let], [exec.bulk], [exec.when.all] |
| Consumers | [exec.consumers] | sync_wait, sync_wait_with_variant |
| Structured concurrency | [exec.scope], [exec.spawn] | counting_scope; associate, spawn, spawn_future |
| Execution contexts | [exec.run.loop], [exec.par.scheduler] | run_loop; parallel_scheduler and its replaceable backend |
| Coroutines | [exec.as.awaitable], [exec.task] | co_await on senders in coroutines; task<T>, affine and more |
A suggested reading order is [exec.async.ops] → [exec.recv], [exec.opstate] → [exec.snd] → [exec.cmplsig] → [exec.connect], then whichever algorithms interest you.
Customization: member functions#
Table 158 in [exec.general]/3 divides the chapter's customization point objects (CPOs, function objects whose behavior user types can customize) into four categories:
| Category | Purpose | Examples |
|---|---|---|
| Core | Connect components together | connect, start |
| Completion functions | Used by senders to announce success, error or cancellation | set_value, set_error, set_stopped |
| Sender algorithms | Customizable factories, adaptors and consumers | just, then, continues_on, sync_wait |
| Queries | Query various properties of an object | get_stop_token, get_scheduler, get_completion_scheduler |
User types take part in customization through ordinary member functions: set_value(rcvr, vs...) calls rcvr.set_value(vs...), connect(s, r) looks for s.connect(r) first, and a query q(env) calls env.query(q). In addition, a type declares a tag (such as using receiver_concept = receiver_tag;) to say it is a receiver, sender or scheduler.
From tag_invoke to member functions
Early revisions of P2300 implemented customization with free functions like tag_invoke, written tag_invoke(set_value, rcvr, v). The version merged into C++26 switched to member functions, which makes writing your own components much simpler.
Exposition-only entities used throughout the chapter#
The standard uses some exposition-only macros and concepts to shorten the wording; they aren't things you can call. The ones you'll meet most often in the later pages are:
¶5 MANDATE-NOTHROW(expr) is equivalent to expr, but requires noexcept(expr) to be true, otherwise the program is ill-formed. Almost every CPO wraps this layer: set_value, start, get_env and the various queries must all be non-throwing. The whole library is designed so that completion paths don't throw; errors must be delivered via set_error.
¶6 movable-valuedecay_t<T> to be move constructible and constructible from T, and T not to be an array. The ts of just(ts...) and the f of then(sndr, f) must satisfy it, because they get stored in the sender.
¶7 MATCHING-SIG(F1, F2) compares two function types after first replacing each parameter type A with A&&. So set_value_t(int) and set_value_t(int&&) count as the same signature, while set_value_t(int&) is different. All matching of completion signatures uses it ([exec.async.ops]/13).
¶8 AS-EXCEPT-PTR(err) turns an error value into an exception_ptr: an exception_ptr is returned as is (it must not be null), an error_code is wrapped in a system_error, and other types go through make_exception_ptr. sync_wait and the coroutine integration use it when they need to throw an error.
¶9 AS-CONST(expr) accesses expr as a const lvalue reference. Queries always call query through it, so a query member must be const.
Header <execution>#
<execution> ([execution.syn]) holds both the C++17 execution policies ([execpol]) and everything in this chapter. Three things are worth noting:
forwarding_query,get_allocatorandget_stop_tokenare in namespacestd, because other library components use them too; the remaining queries are instd::execution.sync_waitandsync_wait_with_variantare instd::this_thread, a reminder that they block the current thread.- The types of
read_env,write_envandunstoppableare unspecified, rather than having namedxxx_ttypes like the other CPOs.
History
Algorithms from early revisions of P2300 that start eagerly or share their result, such as ensure_started, start_detached and split, don't exist in the current draft. Eager starting is now handled by async scopes: spawn and spawn_future must be attached to a scope ([exec.spawn]), so every started operation has someone responsible for waiting until it ends. task, counting_scope, parallel_scheduler and others were added by other proposals after P2300 was merged.
Implementation status
The authors of P2300 maintain a reference implementation, stdexec, where most of this chapter's design was implemented first. Its interface doesn't necessarily match the current draft word for word; watch for differences when you use it to check the examples.
Remaining wording
- [exec.general] ¶1–2: this clause describes components supporting execution of function objects; Table 157 lists only four subclauses (schedulers, receivers, operation states, senders), all in
<execution>. - [execution.syn] ¶1: variant-or-empty
is the deduplicated variant<decay_t<Ts>...>whenTsis non-empty, and a type that can't be constructed when it is empty. That way a sender with no value completions still has a validvalue_types_of_t. - [execution.syn] ¶2–3: single-sender-value-type and single-sender describe senders with exactly one value completion signature:
decay_t<T>for one datum,voidfor none, andtuple<decay_t<Ts>...>for several.co_awaiton a sender ([exec.as.awaitable]) requires a single-sender. - [execution.syn] ¶4: valid-completion-signatures means a specialization of
completion_signatures.
Contents
- 33.1General[exec.general]
- 33.2Queries and queryables[exec.queryable]draft
- 33.3Asynchronous operations[exec.async.ops]draft
- 33.4Header <execution> synopsis[execution.syn]
- 33.5Queries[exec.queries]draft
- 33.5.1Query utilities[exec.queries.expos]
- 33.5.2forwarding_query[exec.fwd.env]
- 33.5.3get_allocator[exec.get.allocator]
- 33.5.4get_stop_token[exec.get.stop.token]
- 33.5.5execution::get_env[exec.get.env]
- 33.5.6execution::get_domain[exec.get.domain]
- 33.5.7execution::get_scheduler[exec.get.scheduler]
- 33.5.8execution::get_start_scheduler[exec.get.start.scheduler]
- 33.5.9execution::get_delegation_scheduler[exec.get.delegation.scheduler]
- 33.5.10execution::get_forward_progress_guarantee[exec.get.fwd.progress]
- 33.5.11execution::get_completion_scheduler[exec.get.compl.sched]
- 33.5.12execution::get_completion_domain[exec.get.compl.domain]
- 33.5.13execution::get_await_completion_adaptor[exec.get.await.adapt]
- 33.6Schedulers[exec.sched]draft
- 33.7Receivers[exec.recv]draft
- 33.8Operation states[exec.opstate]draft
- 33.9Senders[exec.snd]draft
- 33.9.1General[exec.snd.general]
- 33.9.2Exposition-only entities[exec.snd.expos]
- 33.9.3Sender concepts[exec.snd.concepts]
- 33.9.4Awaitable helpers[exec.awaitable]
- 33.9.5execution::indeterminate_domain[exec.domain.indeterminate]
- 33.9.6execution::default_domain[exec.domain.default]
- 33.9.7execution::transform_sender[exec.snd.transform]draft
- 33.9.8execution::apply_sender[exec.snd.apply]
- 33.9.9execution::get_completion_signatures[exec.getcomplsigs]
- 33.9.10execution::connect[exec.connect]draft
- 33.9.11Sender factories[exec.factories]draft
- 33.9.12Sender adaptors[exec.adapt]draft
- 33.9.12.1General[exec.adapt.general]
- 33.9.12.2Closure objects[exec.adapt.obj]
- 33.9.12.3execution::write_env[exec.write.env]
- 33.9.12.4execution::unstoppable[exec.unstoppable]
- 33.9.12.5execution::starts_on[exec.starts.on]
- 33.9.12.6execution::continues_on[exec.continues.on]
- 33.9.12.7execution::schedule_from[exec.schedule.from]
- 33.9.12.8execution::on[exec.on]draft
- 33.9.12.9execution::then, execution::upon_error, execution::upon_stopped[exec.then]
- 33.9.12.10execution::let_value, execution::let_error, execution::let_stopped[exec.let]draft
- 33.9.12.11execution::bulk, execution::bulk_chunked, and execution::bulk_unchunked[exec.bulk]draft
- 33.9.12.12execution::when_all[exec.when.all]draft
- 33.9.12.13execution::into_variant[exec.into.variant]
- 33.9.12.14execution::stopped_as_optional[exec.stopped.opt]
- 33.9.12.15execution::stopped_as_error[exec.stopped.err]
- 33.9.12.16execution::associate[exec.associate]
- 33.9.12.17Exposition-only execution::stop-when[exec.stop.when]
- 33.9.12.18execution::spawn_future[exec.spawn.future]
- 33.9.13Sender consumers[exec.consumers]draft
- 33.10Completion signatures[exec.cmplsig]done
- 33.11Queryable utilities[exec.envs]
- 33.12Execution contexts[exec.ctx]
- 33.13Coroutine utilities[exec.coro.util]
- 33.13.1execution::as_awaitable[exec.as.awaitable]draft
- 33.13.2execution::with_awaitable_senders[exec.with.awaitable.senders]
- 33.13.3execution::affine[exec.affine]
- 33.13.4execution::inline_scheduler[exec.inline.scheduler]
- 33.13.5execution::task_scheduler[exec.task.scheduler]
- 33.13.6execution::task[exec.task]draft
- 33.14Execution scope utilities[exec.scope]draft
- 33.15Parallel scheduler[exec.par.scheduler]draft
- 33.16Namespace parallel_scheduler_replacement[exec.parschedrepl]