Execution control library (since C++26)
The Execution control library provides a framework for managing asynchronous execution on generic execution resources.
The library aims to provide vocabulary types for asynchronous operations and to allow the construction of task execution graphs in a simple, composable way.
Library-wide definitions
- Sender: A description of asynchronous work to be sent for execution. Produces an operation state (below).
- Senders asynchronously “send” their results to listeners called “receivers” (below).
- Senders can be composed into task graphs using generic algorithms.
- Sender factories and adaptors are generic algorithms that capture common async patterns in objects satisfying the sender concept.
- A sender has an associated “queryable object” (below) known as sender attributes that describes various characteristics of the sender and of the asynchronous operation(s) it produces.
- Receiver: A generalized callback that consumes or “receives” the asynchronous results produced by a sender.
- Receivers have three different “channels” through which a sender may propagate results: success, failure, and canceled, so-named “value”, “error”, and “stopped”.
- Receivers provide an extensible execution environment: a “queryable object” (below) that the consumer can use to parameterize the asynchronous operation.
- Operation State: An object that contains the state needed by the asynchronous operation.
- A sender and receiver are connected when passed to the
execution::connectfunction. - The result of connecting a sender and a receiver is an operation state.
- Work is not enqueued for execution until
execution::startis called on an operation state. - Once started, the operation state’s lifetime cannot end before the async operation is complete, and its address must be stable.
- A sender and receiver are connected when passed to the
- Execution Agent: An entity such as a thread that may perform work in parallel with other execution agents
- Implementations or users can introduce other kinds of agents such as processes or thread-pool tasks.
- Execution Resource: Also known as execution context, a program entity that manages a (possibly dynamic) set of execution agents.
- The currently active thread, a system-provided thread pool, and uses of an API associated with an external hardware accelerator are all examples of execution resources.
- Scheduler: A lightweight handle to an execution resource for scheduling work onto that resource.
- A scheduler is a factory for a sender that completes its receiver from an execution agent owned by the execution resource.
- Queryable Object: A read-only collection of key/value pairs where each key is a customization point object known as a “query object”.
- A query is an invocation of a query object with a queryable object as its first argument and a (possibly empty) set of additional arguments. A query imposes syntactic and semantic requirements on its invocations.
Library utilities
Concepts | |
Schedulers | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
specifies that a type is a scheduler, a handle to an execution resource (concept) |
Senders | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
specifies that a type is a sender, a description of asynchronous work (concept) |
(C++26) |
specifies that the ways a sender can complete are known, for a given environment or without one (concept) |
(C++26) |
specifies that the ways a sender can complete depend on the environment it is started in (concept) |
Receivers | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
specifies that a type is a receiver (concept) |
| specifies that a receiver can be recreated from the address of the operation state it was connected to (concept) | |
Operation states | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
specifies that a type is an operation state (concept) |
Asynchronous scopes | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
specifies the requirements for an RAII handle that represents an active association with an asynchronous scope (concept) |
(C++26) |
specifies a non-owning handle to an asynchronous scope that behaves like a reference to asynchronous scope (concept) |
Utility components | |
Execution contexts | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
execution resource holding a thread-safe MPSC task queue and a manually-driven event loop (class) |
Execution domains | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
execution domain of the standard library's own implementations of the sender algorithms (class) |
| execution domain of work that can complete in one of several domains, using the default implementations of the algorithms (class template) | |
(C++26) |
transforms into a new sender under a given execution domain tag (function template) |
(C++26) |
runs an algorithm that consumes a sender with the implementation of a given execution domain (function template) |
Forward progress guarantee | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
| specifies a forward progress guarantee of execution agents created by the scheduler's associated execution resource (enum) | |
Environments | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
builds a queryable object from a query object and a value (class template) |
(C++26) |
aggregates several queryable objects into one queryable object (class template) |
(C++26)(C++26) |
returns the associated queryable object for its given argument (customization point object) |
Queries | |
Defined in header
<execution> | |
Defined in namespace
std | |
(C++26) |
asks a query object whether it should be forwarded through queryable adaptors (customization point object) |
(C++26) |
asks a queryable object for its associated allocator (customization point object) |
(C++26)(C++26) |
asks a queryable object for the stop token of an asynchronous operation, or obtains the type of that token (customization point object) |
Defined in namespace
std::execution | |
(C++26) |
asks a queryable object for its associated execution domain tag (customization point object) |
| asks the completion domain associated with a completion tag from a sender's attributes (customization point object) | |
(C++26) |
asks a queryable object for its associated scheduler (customization point object) |
| asks a queryable object for the scheduler an operation will be or was started on (customization point object) | |
| asks a queryable object for a scheduler that can be used to delegate work to for the purpose of forward progress delegation (customization point object) | |
| obtains the completion scheduler associated with a completion tag from a sender's attributes (customization point object) | |
| asks a scheduler about its execution::forward_progress_guarantee (customization point object) | |
| asks a queryable object for its associated awaitable completion adaptor (customization point object) | |
Completion signatures | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
| lists the ways a sender can complete (class template) | |
| obtains the completion signatures of a sender, or their type (function) | |
| exception thrown when the completion signatures of a sender are requested without the environment they depend on (class) | |
(C++26) |
obtains the tag type of a sender (alias template) |
(C++26) |
obtains the value completion type of a sender (alias template) |
(C++26) |
obtains the error completion type of a sender (alias template) |
(C++26) |
determines whether the sender supports stopped completion (variable template) |
Coroutine utility | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
transforms an expression into awaitable object within a particular coroutine (customization point object) |
| when used as the base class of a coroutine promise type, enables senders to be awaitable within that coroutine type (class template) | |
(C++26) |
adapts a sender into one that completes on the scheduler the operation was started on (customization point object) |
(C++26) |
a trivial scheduler that completes immediately and synchronously on the thread that calls std::execution::start on the operation state produced by its sender (class) |
(C++26) |
represents a sender that can be used as the return type of coroutine functions (class template) |
(C++26) |
holds an error that a task coroutine passes to co_yield to complete with that error (class template) |
(C++26) |
provides a uniform, type-erased interface, which allows to store, pass around, and invoke scheduling operations on arbitrary infallible schedulers (class) |
Asynchronous scopes | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
| a lightweight, reference-counted asynchronous scope. It tracks how many dynamically spawned asynchronous operations are currently running (class) | |
(C++26) |
a reference-counted asynchronous scope like simple_counting_scope, which can also request stop for all the asynchronous operations it tracks (class) |
Parallel scheduler | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
| represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution (class) | |
obtains a parallel_scheduler for the shared execution resource that runs work in parallel (function) | |
Defined in namespace
| |
interface through which a parallel_scheduler_backend completes an operation and queries its receiver (class) | |
used for bulk_chunked and bulk_unchunked customizations that will also receive notifications from implementations of parallel_scheduler_backend corresponding to different iterations (class) | |
serves as the abstraction bridge between the user-facing parallel_scheduler and the backend execution resource (such as a OS-provided system thread pool) (class) | |
returns the backend object that every parallel_scheduler uses; a program can replace it to provide its own backend (function) | |
Core operations | |
Operation state | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
connects a sender with a receiver (customization point object) |
(C++26) |
starts the asynchronous operation associated with an operation_state object (customization point object) |
Completion functions | |
| These functions are called by senders to announce the completion of the work to their receivers. | |
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
value completion function indicating successful completion (customization point object) |
(C++26) |
error completion function indicating that an error occurred during calculation or scheduling (customization point object) |
(C++26) |
stopped completion function indicating that an operation ended before it could achieve success or failure (customization point object) |
Sender algorithms
Sender algorithms are the building blocks of asynchronous work. Each one creates a sender, or combines senders into a new one, and a computation is described by building a chain or graph of them and then starting it.
Sender factories and sender adaptors only describe work: apart from copying or moving their arguments, they do nothing until the sender they return is connected to a receiver and started, usually by a sender consumer. spawn_future is the exception: it starts its work right away.
Most sender adaptors can be applied with operator|, so that a chain reads in the order in which the work happens: just(42) | then(f) | continues_on(sch) is the same as continues_on(then(just(42), f), sch).
Mistakes in a chain, such as a function that cannot be called with the values that the previous step sends, are diagnosed at compile time where the chain is built, as long as what the senders send does not depend on where they are started; this is true of most senders. Otherwise, they are diagnosed when the sender is connected, for example in this_thread::sync_wait.
The algorithms can be customized for an execution resource: a scheduler can provide its own implementation of an algorithm, which is used for work that runs on that scheduler. For example, parallel_scheduler provides an implementation of bulk that runs the iterations in parallel.
Sender factories |
A sender factory is an algorithm that takes no senders and returns a sender. Factories begin a chain of work.
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
obtains a sender that completes, with no values, on an execution agent of a given scheduler (customization point object) |
(C++26) |
returns a sender that immediately sends copies of the given values (customization point object) |
(C++26) |
returns a sender that immediately sends a copy of the given error (customization point object) |
(C++26) |
returns a sender that immediately completes with the stopped signal (customization point object) |
(C++26) |
creates a sender that queries its receiver's associated environment (customization point object) |
Pipeable sender adaptors | |
Most sender adaptors can also be called without their sender argument. The result is a pipeable sender adaptor closure object c, such as then(f): sndr | c and c(sndr) are equivalent. Two closures can be combined into one: c | d is a closure that applies c and then d, so a reusable piece of a chain can be given a name. A program can define its own pipeable adaptors by deriving from sender_adaptor_closure.
Defined in header
<execution> | |
Defined in namespace
std::execution | |
| helper base class template for defining a pipeable sender adaptor closure object (class template) | |
Sender adaptors | |
A sender adaptor is an algorithm that takes one or more senders and returns a sender built from them. When the returned sender is started, it starts the senders it was given as needed, and its completion depends on theirs.
Defined in header
<execution> | |
Defined in namespace
std::execution | |
(C++26) |
adapts a sender so that the operations inside it see extra or different values in their execution environment (customization point object) |
(C++26) |
shields the provided sender from external cancellation requests, ensuring that the critical task runs uninterrupted to completion (customization point object) |
(C++26) |
adapts a provided sender into one that will start an execution on the provided scheduler's execution resource (customization point object) |
(C++26) |
adapts a provided sender into one that completes on the provided scheduler's execution resource (customization point object) |
(C++26) |
a customization hook for how work leaves a scheduler's execution context (completes like the sender it wraps) (customization point object) |
(C++26) |
adapts a sender to run it (or a continuation of it) on the provided scheduler, then return to the scheduler it came from (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and sending its result on (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the error if the sender fails and sending its result on as a value (customization point object) |
(C++26) |
chains a function onto a sender, calling it if the sender is cancelled and sending its result on as a value (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and continuing with the sender it returns (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the error if the sender fails and continuing with the sender it returns (customization point object) |
(C++26) |
chains a function onto a sender, calling it if the sender is cancelled and continuing with the sender it returns (customization point object) |
(C++26)(C++26)(C++26) |
runs a function for every index in a range after a sender completes, so the work can be spread over a scheduler's execution resources (customization point object) |
(C++26) |
adapts multiple input senders into a sender that completes once all of the input senders have completed (customization point object) |
| adapts multiple input senders, with each possibly having multiple completion signatures, into a sender that completes once all of the input senders have completed (customization point object) | |
(C++26) |
adapts a sender that can complete with several different sets of values into one that completes with a single std::variant of tuples (customization point object) |
returns a sender that maps the value channel to std::optional<std::decay_t<T>> and the stopped channel to std::nullopt(customization point object) | |
(C++26) |
returns a sender that maps the stopped channel to an error (customization point object) |
(C++26) |
tries to associate a sender with an asynchronous scope such that the scope can track the lifetime of any asynchronous operations created with the sender (customization point object) |
(C++26) |
eagerly starts a sender within an asynchronous scope and returns a sender that completes with its result, or stopped if the scope did not accept it (customization point object) |
Sender consumers | |
A sender consumer is an algorithm that takes one or more senders and does not return a sender. Consumers start the work: this_thread::sync_wait waits for it and returns its result to ordinary code, and spawn starts it in an async scope without waiting.
Defined in header
<execution> | |
Defined in namespace
std::this_thread | |
(C++26) |
blocks current thread until the specified sender completes and returns its async result (customization point object) |
| blocks current thread until the specified sender with possibly multiple completion signatures completes and returns its async result (customization point object) | |
Defined in namespace
std::execution | |
(C++26) |
eagerly launches the given sender within an asynchronous scope, returning immediately without waiting for it to finish (customization point object) |
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_counting_scope |
202506L |
(C++26) | Async scopes: creating scopes for non-sequential concurrency |
__cpp_lib_parallel_scheduler |
202506L |
(C++26) | Parallel scheduler |
__cpp_lib_senders |
202406L |
(C++26) | std::execution: Sender-receiver model of execution control |
202506L |
(C++26) | std::execution: More extensions | |
__cpp_lib_task |
202506L |
(C++26) | Coroutine task types |
Example
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <cstdio>
#include <execution>
#include <string>
#include <thread>
#include <utility>
using namespace std::literals;
int main()
{
std::execution::run_loop loop;
std::jthread worker([&](std::stop_token st)
{
std::stop_callback cb{st, [&]{ loop.finish(); }};
loop.run();
});
std::execution::sender auto hello = std::execution::just("hello world"s);
std::execution::sender auto print
= std::move(hello)
| std::execution::then([](std::string msg)
{
return std::puts(msg.c_str());
});
std::execution::scheduler auto io_thread = loop.get_scheduler();
std::execution::sender auto work = std::execution::on(io_thread, std::move(print));
auto [result] = std::this_thread::sync_wait(std::move(work)).value();
return result;
}
Output:
hello world
See also
(C++11) |
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result (function template) |
External links
| 1. | NVIDIA/stdexec — A reference implementation — github.com
|
| 2. | bemanproject/execution — Another reference implementation — github.com
|
| 3. | libunifex — Yet another experimental implementation — github.com
|
| 4. | What are Senders Good For, Anyway? — Eric Niebler, 2024 |
| 5. | Senders/Receivers: An Introduction — Lucian Radu Teodorescu, 2024 |