Execution control library (since C++26) (original) (raw)
Execution control library
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.
Contents
- 1 Library-wide definitions
- 2 Library utilities
- 2.1 Concepts
* 2.1.1 Schedulers
* 2.1.2 Senders
* 2.1.3 Receivers
* 2.1.4 Operation states - 2.2 Utility components
* 2.2.1 Execution contexts
* 2.2.2 Execution domains
* 2.2.3 Forward progress guarantee
* 2.2.4 Environments
* 2.2.5 Queries
* 2.2.6 Completion signatures
* 2.2.7 Coroutine utility - 2.3 Core operations
* 2.3.1 Operation state
* 2.3.2 Completion functions - 2.4 Sender algorithms
* 2.4.1 Sender factories
* 2.4.2 Pipeable sender adaptors
* 2.4.3 Sender adaptors
* 2.4.4 Sender consumers - 2.5 Example
- 2.6 See also
- 2.1 Concepts
[edit] 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.
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 set of key/value pairs 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 std::execution::connect function.
The result of connecting a sender and a receiver is an operation state.
Work is not enqueued for execution until “
start
” is 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.
Scheduler: A lightweight handle to an execution context.
An execution context is a source of asynchronous execution such as a thread pool or a GPU stream.
A scheduler is a factory for a sender that completes its receiver from a thread of execution owned by the execution context.
[edit] Library utilities
[edit] Concepts
[edit] Schedulers
[edit] Senders
[edit] Receivers
[edit] Operation states
[edit] Utility components
[edit] Execution contexts
[edit] Execution domains
Defined in header | |
---|---|
Defined in namespace std::execution | |
execution::default_domain(C++26) | default execution domain tag type that dispatches transformations from a sender tag (class) [edit] |
execution::transform_sender(C++26) | transforms into a new sender under a given execution domain tag (function template) [edit] |
execution::transform_env(C++26) | transforms into a new queryable object under a given execution domain tag (function template) [edit] |
execution::apply_sender(C++26) | consumes a sender using a given sender consumer tag with a set of arguments and returns its result under a given execution domain tag (function template) [edit] |
[edit] Forward progress guarantee
[edit] Environments
Defined in namespace std::execution |
[edit] Queries
Defined in header | |
---|---|
forwarding_query(C++26) | asks a query object whether it should be forwarded through queryable adaptors(customization point object)[edit] |
get_allocator(C++26) | asks a queryable object for its associated allocator(customization point object)[edit] |
get_stop_token(C++26) | asks a queryable object for its associated stop token(customization point object)[edit] |
execution::get_domain(C++26) | asks a queryable object for its associated execution domain tag(customization point object)[edit] |
execution::get_scheduler(C++26) | asks a queryable object for its associated scheduler(customization point object)[edit] |
execution::get_delegation_scheduler(C++26) | 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)[edit] |
execution::get_completion_scheduler(C++26) | obtains the completion scheduler associated with a completion tag from a sender's attributes(customization point object)[edit] |
execution::get_forward_progress_guarantee(C++26) | asks a scheduler about its execution::forward_progress_guarantee(customization point object)[edit] |
[edit] Completion signatures
Defined in header | |
---|---|
Defined in namespace std::execution | |
execution::completion_signatures(C++26) | type that encodes a set of completion signatures (class template) [edit] |
execution::get_completion_signatures(C++26) | obtains the completion signatures of a sender(customization point object)[edit] |
execution::transform_completion_signatures(C++26) | transforms one set of completion signatures into another(alias template)[edit] |
execution::transform_completion_signatures_of(C++26) | transforms completion signatures of a sender(alias template)[edit] |
execution::tag_of_t(C++26) | obtains the tag type of a sender(alias template)[edit] |
execution::value_types_of_t(C++26) | obtains the value completion type of a sender(alias template)[edit] |
execution::error_types_of_t(C++26) | obtains the error completion type of a sender(alias template)[edit] |
execution::sends_stopped(C++26) | determines whether the sender supports stopped completion(variable template)[edit] |
[edit] Coroutine utility
[edit] Core operations
[edit] Operation state
[edit] Completion functions
These functions are called by senders to announce the completion of the work to their receivers.
Defined in namespace std::execution |
[edit] Sender algorithms
[edit] Sender factories
A sender factory is a function that returns a sender and whose parameters have types for which the sender concept is false.
The following are sender factories:
Defined in header | |
---|---|
Defined in namespace std::execution | |
execution::just(C++26) | Accepts a variadic number of arguments and returns a sender that, when connected and started, completes synchronously by passing the arguments to the receiver's value completion function(customization point object)[edit] |
execution::just_error(C++26) | Accepts a single argument and returns a sender that, when connected and started, completes synchronously by passing the argument to the receiver's error completion function(customization point object)[edit] |
execution::just_stopped(C++26) | creates a sender that completes immediately by calling its receiver's set_stopped(customization point object)[edit] |
execution::read_env(C++26) | creates a sender that queries its receiver's associated environment(customization point object)[edit] |
execution::schedule(C++26) | prepares a task graph for execution on a given scheduler(customization point object)[edit] |
[edit] Pipeable sender adaptors
[edit] Sender adaptors
A sender adaptor is a function returning a sender whose parameters include at least one whose type satisfies the sender concept, and for which the returned sender is a parent sender of the adaptor function's sender arguments.
The following are sender adaptors:
Defined in header | |
---|---|
Defined in namespace std::execution | |
execution::starts_on(C++26) | adapts a provided sender into one that will start an execution on the provided scheduler's execution resource(customization point object)[edit] |
execution::continues_on(C++26) | adapts a provided sender into one that completes on the provided scheduler's execution resource(customization point object)[edit] |
execution::on(C++26) | adapts a provided sender to transfer execution to a provided scheduler's execution resource on which the sender or the continuation runs, and then transfers execution back to the original resource(customization point object)[edit] |
execution::schedule_from(C++26) | schedules work dependent on the completion of a provided sender onto a provided scheduler's execution resource(customization point object)[edit] |
execution::then(C++26) | chains the task graph by the input sender with a node represents invoking the provided function with the values sent by the input sender as arguments(customization point object)[edit] |
execution::upon_error(C++26) | chains the task graph by the input sender with a node representing invoking the provided function with the error sent by the input sender if an error occurred(customization point object)[edit] |
execution::upon_stopped(C++26) | chains the task graph by the input sender with a node representing invoking the provided function with the stopped behavior by the input sender if a "stopped" signal is sent(customization point object)[edit] |
execution::let_value(C++26) | returns a sender which represents a node chained to the input sender, which when started, invokes the provided function with the values sent by the input sender as arguments(customization point object)[edit] |
execution::let_error(C++26) | returns a sender which represents a node chained to the input sender, which invokes the provided function with the error from the input sender, if occurred(customization point object)[edit] |
execution::let_stopped(C++26) | returns a sender which represents a node chained to the input sender, which invokes the provided function with the stop token from the input sender, if the "stopped" signal is sent(customization point object)[edit] |
execution::bulkexecution::bulk_chunkedexecution::bulk_unchunked(C++26) | creates a multi-shot sender that invokes the function with every index in the provided shape along with the values sent by the input sender. The sender completes once all invocations have completed, or an error has occurred(customization point object)[edit] |
execution::split(C++26) | if the provided sender is a multi-shot sender, returns that sender, otherwise, returns a multi-shot sender which sends values equivalent to the values sent by the provided sender(customization point object)[edit] |
execution::when_all(C++26) | adapts multiple input senders into a sender that completes once all of the input senders have completed(customization point object)[edit] |
execution::when_all_with_variant(C++26) | 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)[edit] |
execution::into_variant(C++26) | returns a sender which sends a variant of tuples of all the possible sets of types sent by the input sender(customization point object)[edit] |
execution::stopped_as_optional(C++26) | 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)[edit] |
execution::stopped_as_error(C++26) | returns a sender that maps the stopped channel to an error(customization point object)[edit] |
[edit] Sender consumers
A sender consumer is an algorithm that takes one or more senders as parameters and that does not return a sender.
Defined in namespace std::this_thread |
[edit] Example
A version of this example is available on godbolt.org, where it uses stdexec, an experimental reference implementation of std::execution.
#include #include #include #include #include using namespace std::literals; int main() { std::execution::run_loop loop; std::jthread worker(& 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( 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:
[edit] See also
| | runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result (function template) [edit] | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |