Difference between revisions of "cpp/execution"
m (Cubbi moved page cpp/experimental/execution to cpp/execution: this isn't a TS, why was it in /experimental/?) |
|||
Line 38: | Line 38: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc header| | + | {{dsc header|execution}} |
{{dsc namespace|std::execution}} | {{dsc namespace|std::execution}} | ||
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc just}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc just_error}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc just_stopped}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc read}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc schedule}} |
{{dsc end}} | {{dsc end}} | ||
Line 53: | Line 53: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc header| | + | {{dsc header|execution}} |
{{dsc namespace|std::execution}} | {{dsc namespace|std::execution}} | ||
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc bulk}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc ensure_started}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc into_variant}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc let_error}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc let_value}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc let_stopped}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc on}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc split}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc stopped_as_error}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc stopped_as_optional}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc then}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc transfer}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc upon_error}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc upon_stopped}} |
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc when_all}} |
{{dsc end}} | {{dsc end}} | ||
Line 78: | Line 78: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc header| | + | {{dsc header|execution}} |
{{dsc namespace|std::execution}} | {{dsc namespace|std::execution}} | ||
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc start_detached}} |
{{dsc h2|Functions managing the current thread}} | {{dsc h2|Functions managing the current thread}} | ||
{{dsc header|experimental/execution}} | {{dsc header|experimental/execution}} | ||
{{dsc namespace|std::this_thread}} | {{dsc namespace|std::this_thread}} | ||
− | {{dsc inc|cpp | + | {{dsc inc|cpp/thread/dsc sync_wait}} |
{{dsc end}} | {{dsc end}} | ||
Line 91: | Line 91: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc header| | + | {{dsc header|execution}} |
{{dsc namespace|std::execution}} | {{dsc namespace|std::execution}} | ||
− | {{dsc inc|cpp | + | {{dsc inc|cpp/execution/dsc execute}} |
{{dsc end}} | {{dsc end}} | ||
Revision as of 13:09, 3 October 2024
The Execution library provides a framework for managing asynchronous execution on generic execution resources, targeting the standard C++ library.
The library aims to provide vocabulary types for async operations and to allow the construction of task execution graphs in a simple, composable way.
Contents |
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.
Library utilities
Senders
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
<execution> | |
Defined in namespace
std::execution | |
(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) |
(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) |
(C++26) |
creates a sender that completes immediately by calling its receiver's set_stopped (customization point object) |
(C++26) |
prepares a task graph for execution on a given scheduler (customization point object) |
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
<execution> | |
Defined in namespace
std::execution | |
(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) |
Eagerly starts a sender, returning a sender that will deliver the results to a receiver to which it is connected and started, if any. When the result sender is not connected to a receiver, or if the resulting operation state is not started, the results are ignored. If such a sender is destroyed before the underlying operation completes, the operation continues running detached. (function template) | |
(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) |
(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) |
(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) |
(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) |
(C++26) |
start the provided sender on an execution agent belonging to the execution resource associated with the provided scheduler (customization point object) |
(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) |
(C++26) |
returns a sender that maps the stopped channel to an error (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) |
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) |
Returns a sender describes transition from the execution context of the input sender to the execution context of the target scheduler (function template) | |
(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) |
(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) |
(C++26) |
completes once all of the input senders have completed (customization point object) |
Sender consumers
A sender consumer is an algorithm that takes one or more senders as parameters and that does not return a sender.
The following are sender consumers:
Defined in header
<execution> | |
Defined in namespace
std::execution | |
Completes when the provided sender completes, or calls std::terminate if the sender sends an error. (function template) | |
Functions managing the current thread | |
Defined in header
<experimental/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) |
Eager execution
Allows a fire-and-forget eager submission of an invocable to a scheduler.
Defined in header
<execution> | |
Defined in namespace
std::execution | |
Submits the provided function for execution on the provided scheduler. (function template) |
Example
A version of this example is available on godbolt.org, where it uses stdexec, an experimental reference implementation of std::execution.
#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) { std::puts(msg.c_str()); return 0; }); 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) |