Namespaces
Variants

std::execution::let_stopped

From cppreference.com
 
 
Execution control library
Concepts
Utility components
Execution contexts
Execution domains
Forward progress guarantee
Environments
    
    
    
Queries
Completion signatures
Pipeable sender adaptors
Coroutine utility
 
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:

  • f is 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 f throws, the returned sender completes with the exception as an error (set_error with 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. f is called only after the returned sender has been connected to a receiver and started, and only if sndr completes with the stopped signal.
  • f is called on the execution agent on which sndr completed with the stopped signal, and the sender it returns is started there.
  • The sender returned by f is a new operation, started with the environment of the whole let_stopped sender: 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 on sndr (for example with write_env around sndr alone) 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 as sndr. 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 f is noexcept, the returned sender can also complete with a std::exception_ptr error, and that is part of its type even if f never 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. Declare f noexcept if it cannot throw. The same applies if connecting the sender returned by f can 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

chains a function onto a sender, calling it if the sender is cancelled and sending its result on as a value
(customization point object)[edit]
chains a function onto a sender, calling it with the values the sender sends and continuing with the sender it returns
(customization point object)[edit]
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)[edit]
returns a sender that maps the stopped channel to an error
(customization point object)[edit]
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]