std::execution::let_stopped
| Defined in header <execution>
|
||
struct let_stopped_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr let_stopped_t let_stopped{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto let_stopped( execution::sender auto sndr,
std::invocable auto f );
|
(3) | (since C++26) |
Recovers from cancellation with more asynchronous work: when sndr completes with the stopped signal (it was cancelled, typically because stop was requested through its stop token), f is called, and f returns a sender. That sender is started, and its completion becomes the completion of the returned sender.
By default a stopped completion travels down a pipeline skipping everything in between, so the whole operation ends up cancelled. let_stopped is how a pipeline carries on after a cancellation with work that is itself asynchronous: loading a cached or default value, releasing a resource through an asynchronous call, or reporting the cancellation. It is the stopped-channel counterpart of let_value and let_error, and the asynchronous counterpart of upon_stopped, whose function returns a plain value.
Like upon_stopped, and unlike let_value and let_error, the function takes no arguments: the stopped signal carries no data.
let_stopped is a pipeable sender adaptor: sndr | let_stopped(f) is equivalent to let_stopped(sndr, f).
Parameters
| sndr | - | the sender whose stopped completion is handled |
| f | - | a function (or other callable) called with no arguments when sndr completes with the stopped signal, which returns a sender
|
Return value
A sender that behaves like sndr until sndr completes with the stopped signal. Then:
fis called, and the sender it returns is started.- The returned sender completes the way that inner sender does: with its values, with its error, or stopped again. If the inner sender succeeds, the cancellation is gone.
- 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 let_value and let_error to continue with a sender after those.
let_stopped(sndr, f) is ill-formed if sndr does not satisfy execution::sender, if f cannot be copied or moved into the sender (it must be a movable value, and not an array), or if f is not callable with no arguments. It is also diagnosed at compile time if f does not return a sender.
Customization point objects
The name execution::let_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::let_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. fis called on the execution agent on whichsndrcompleted with the stopped signal, and the sender it returns is started there.- The sender returned by
fis a new operation, started with the environment of the wholelet_stoppedsender: the environment of the receiver it is connected to. In particular it sees the stop token of that environment. A stop token that was set only onsndr(for example with write_env aroundsndralone) is not part of that environment, so the recovery sender does not see it and is not cancelled by it. If stop is requested on the environment of the whole pipeline, the recovery sender sees the request through that token.
Notes
- After
let_stopped, the cancellation is gone if the returned sender succeeds. To treat a completed and a cancelled operation alike afterwards, return a sender that completes with the same type of value assndr. A sender with two different value types cannot be passed to this_thread::sync_wait, which requires a single set of values. - For a plain computation, upon_stopped is simpler. To turn cancellation into an error, use stopped_as_error; to turn it into an empty std::optional, use stopped_as_optional.
- 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.
- Unless
fisnoexcept, the returned sender can also complete with a std::exception_ptr error, and that is part of its type even iffnever throws. What follows must then handle that error too: a function passed to upon_error has to accept a std::exception_ptr, and spawn does not accept the sender. Declarefnoexceptif it cannot throw. The same applies if connecting the sender returned byfcan throw.
Example
A job is scheduled on an event loop while stop has already been requested through the environment's stop token, so it is cancelled before it starts. let_stopped then starts a different asynchronous operation, a lookup of a default, and the pipeline continues with its result.
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 <print>
#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("fresh result"); });
// The recovery is asynchronous too: a stand-in for loading a cached value.
auto load_default = [] { return ex::just(std::string("default result")); };
auto sndr = ex::write_env(std::move(job),
ex::prop{std::get_stop_token, stop_source.get_token()})
| ex::let_stopped([&]
{
std::println("cancelled, loading the default");
return load_default();
});
auto [result] = std::this_thread::sync_wait(std::move(sndr)).value();
std::println("{}", result);
loop.finish();
}
Output:
cancelled, loading the default
default result
See also
(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) |
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) |