std::execution::upon_stopped
| Defined in header <execution>
|
||
struct upon_stopped_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr upon_stopped_t upon_stopped{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto upon_stopped( execution::sender auto sndr,
/*movable-value*/ auto f );
|
(3) | (since C++26) |
Attaches a handler for cancellation to a sender: when sndr completes with the stopped signal (it was cancelled, typically because stop was requested through its stop token), f is called, and the result of the call becomes a value the returned sender completes with.
A stopped completion is neither a result nor a failure: it says "this was cancelled", and by default it travels down a pipeline skipping everything in between, which makes the whole operation end up cancelled. upon_stopped is how a pipeline recovers from that, for example by supplying a default or marking the work as cancelled. It is the stopped-channel counterpart of then (values) and upon_error (errors).
Unlike then and upon_error, the function takes no arguments: the stopped signal carries no data.
upon_stopped is a pipeable sender adaptor: sndr | upon_stopped(f) is equivalent to upon_stopped(sndr, f).
Parameters
| sndr | - | the sender whose stopped completion is handled |
| f | - | the function (or other callable) to call, with no arguments, when sndr completes with the stopped signal
|
Return value
A sender that behaves like sndr, except that a stopped completion of sndr is replaced by a call of f:
- The result of
fis sent as a value of the returned sender, so the cancellation is gone. Iffreturnsvoid, the returned sender completes with no values. - If
fthrows, the returned sender completes with the exception as an error (set_errorwith a std::exception_ptr).
Value and error completions of sndr are forwarded unchanged: f is not called for them. Use then and upon_error to handle those.
upon_stopped(sndr, f) is ill-formed if sndr does not satisfy execution::sender or f cannot be copied or moved into the sender (it must be a movable value, and not an array). It is also diagnosed at compile time if f cannot be called with no arguments.
Customization point objects
The name execution::upon_stopped denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::upon_stopped_t. See CustomizationPointObject for details.
Details
- Nothing runs when the sender is built.
fis called only after the returned sender has been connected to a receiver and started, and only ifsndrcompletes with the stopped signal. fruns on the execution agent on whichsndrcompleted with the stopped signal.upon_stoppednever moves work to another thread.
Notes
- After
upon_stopped, the cancellation has become a value. If the input sender also completes with values, the returned sender has both kinds of value completion, and they must agree to be useful: to treat a completed and a cancelled operation alike afterwards, makefreturn the same type as the values ofsndr. A sender with two different value types cannot be passed to this_thread::sync_wait, which requires a single set of values. - To turn cancellation into an error instead, so that it is reported through the error channel, use stopped_as_error. To turn it into an empty std::optional, use stopped_as_optional. To handle it by running another asynchronous operation, use let_stopped, whose function returns a sender.
- Stop requests come from the stop token in the environment of the receiver the sender is connected to, for example one provided by the algorithm that starts the operation.
Example
A job is scheduled on an event loop while stop has already been requested through the environment's stop token, so it completes with the stopped signal and never runs. upon_stopped turns the cancellation into a value that tells the caller what happened.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.
#include <execution>
#include <iostream>
#include <stop_token>
#include <string>
#include <thread>
namespace ex = std::execution;
int main()
{
ex::run_loop loop;
std::jthread worker([&] { loop.run(); });
std::inplace_stop_source stop_source;
stop_source.request_stop(); // the operation is cancelled before it starts
auto job = ex::schedule(loop.get_scheduler())
| ex::then([] { return std::string("job finished"); });
auto sndr = ex::write_env(std::move(job),
ex::prop{std::get_stop_token, stop_source.get_token()})
| ex::upon_stopped([] { return std::string("job was cancelled"); });
auto [message] = std::this_thread::sync_wait(std::move(sndr)).value();
std::cout << message << '\n';
loop.finish();
}
Output:
job was cancelled
See also
(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 continuing with the sender it returns (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) |