std::execution::unstoppable
| Defined in header <execution>
|
||
inline constexpr /* unspecified */ unstoppable{};
|
(1) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto unstoppable( execution::sender auto sndr );
|
(2) | (since C++26) |
Protects a sender from cancellation: the operations inside the returned sender are given a stop token that never reports a stop request, so a request to stop the surrounding operation cannot reach them, and they run to completion.
Stop requests are how an operation is asked to finish early, and most work should honor them. Some work must not: cleanup that releases a resource, a step that restores an invariant the program broke a moment ago, the last write of a transaction. If cancellation interrupted such work, the program would be left in an inconsistent state, so the work is wrapped in unstoppable and always completes. The rest of the pipeline stays cancellable.
Parameters
| sndr | - | the sender to protect from stop requests |
Return value
A sender that behaves like sndr, with sndr (and everything nested in it) connected to a receiver whose environment is that of the receiver the returned sender is connected to, except that get_stop_token yields a std::never_stop_token: a token that can never be stopped. All completions of sndr are forwarded unchanged.
unstoppable(sndr) is expression-equivalent to write_env(sndr, prop(get_stop_token, never_stop_token{})); see write_env.
Customization point objects
The name execution::unstoppable denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.
Details
- Nothing runs when the sender is built. The token is replaced when the returned sender is connected to a receiver.
- Everything started inside
sndrsees the never-stopping token, including senders returned by functions passed to let_value and its relatives. A stop request made on the outer token while the work runs is not visible inside. - All other queries in the environment, such as the scheduler and the allocator, are forwarded unchanged.
Notes
- Protection applies only to the sender that is wrapped. Operations outside it, before or after, remain cancellable, so a cancelled pipeline can still stop between the wrapped part and the rest. Wrap exactly the work that has to finish.
unstoppableprevents cancellation requests from reaching the work. It does not make the work non-failing: errors still propagate, and the sender can still complete with the stopped signal if the work itself produces one.- A typical use pairs it with an algorithm that runs a cleanup step after another sender whatever the outcome of the first: the cleanup is wrapped in
unstoppableso that cancelling the whole operation cannot skip it. For example, work that must temporarily break an invariant restores it in an unstoppable step, even if the work in between is cancelled or throws. unstoppableis a thin application of write_env, which is the general way to change what the operations inside a sender see in their environment.
Example
A flush of buffered data is scheduled on a worker thread while stop has already been requested. Without unstoppable the flush is cancelled before it runs, which would lose the data. With it, the flush ignores the request and completes.
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;
// An event loop on its own thread; the destructor finishes the loop so that the
// thread can be joined.
struct loop_thread
{
ex::run_loop loop;
std::jthread thread;
loop_thread() : thread([this] { loop.run(); }) {}
~loop_thread() { loop.finish(); }
};
int main()
{
loop_thread worker;
std::inplace_stop_source stop_source;
stop_source.request_stop(); // cancellation is requested before the work starts
auto flush = [&]
{
return ex::schedule(worker.loop.get_scheduler())
| ex::then([] { return std::string("flushed"); });
};
auto cancellable = std::this_thread::sync_wait(
ex::write_env(flush(), ex::prop{std::get_stop_token, stop_source.get_token()}));
auto protected_flush = std::this_thread::sync_wait(
ex::write_env(ex::unstoppable(flush()),
ex::prop{std::get_stop_token, stop_source.get_token()}));
std::println("without unstoppable: {}",
cancellable ? std::get<0>(*cancellable) : std::string("cancelled"));
std::println("with unstoppable: {}",
protected_flush ? std::get<0>(*protected_flush) : std::string("cancelled"));
}
Output:
without unstoppable: cancelled
with unstoppable: flushed
See also
(C++26) |
adapts a sender so that the operations inside it see extra or different values in their execution environment (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) |
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) |