std::execution::set_stopped
| Defined in header <execution>
|
||
struct set_stopped_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr set_stopped_t set_stopped{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Rcvr >
void set_stopped( Rcvr&& rcvr ) noexcept;
|
(3) | (since C++26) |
The stopped completion function: set_stopped(std::move(rcvr)) tells the receiver rcvr that the asynchronous operation it was connected to ended early, without a result, because it was asked to stop. A stopped completion is neither a success nor a failure, and has no arguments. It is how an operation reports that it was cancelled.
An operation completes by calling exactly one of the three completion functions on its receiver: set_value on success, set_error on failure, or set_stopped when it was stopped before it could finish. The sender algorithms and the receivers of the standard library call them; a program calls set_stopped itself when it writes its own sender, in the operation state that sender returns from connect.
Stop is requested through the stop token in the environment of the receiver, std::get_stop_token(execution::get_env(rcvr)). An operation that supports stopping checks the token, or registers a stop callback with it, and completes with set_stopped when stop has been requested. Supporting stop requests is optional: an operation can ignore them and complete as usual. A stop request can also come too late, after the operation has finished its work, so a stop request does not guarantee a stopped completion.
The library reacts to a stopped completion as follows:
- most sender adaptors pass it on unchanged and skip their own work: the function passed to then is not called;
- upon_stopped and let_stopped handle it, by turning it into a value or by starting other work, and stopped_as_optional and stopped_as_error turn it into an empty std::optional or into an error;
- when_all requests stop of the other senders it runs when one of them is stopped, and completes with
set_stopped, unless one of them has failed, in which case it completes with the error; - this_thread::sync_wait returns an empty std::optional;
co_awaiton a sender that completes withset_stoppedin a task does not resume the coroutine: the coroutine is destroyed, and the task completes withset_stoppeditself.
set_stopped_t is the completion tag of stopped completions. It is used to name them, for example:
- in completion signatures:
set_stopped_t()means that a sender can complete withset_stopped(std::move(rcvr)); see completion_signatures and sends_stopped; - in queries:
get_completion_scheduler<set_stopped_t>asks where a sender completes when it is stopped; see get_completion_scheduler.
set_stopped(rcvr) is expression-equivalent to rcvr.set_stopped(). It is ill-formed if rcvr is an lvalue or a const rvalue: a receiver is passed as a non-const rvalue, usually with std::move, because completing uses it up. The program is ill-formed if the expression rcvr.set_stopped() is well-formed but can throw an exception, or has a type other than void.Parameters
| rcvr | - | the receiver of the operation that completes |
Customization point objects
The name execution::set_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::set_stopped_t. See CustomizationPointObject for details.
Notes
- A receiver accepts a stopped completion by providing a
noexceptmember functionset_stopped()that returnsvoid; see receiver. It may be declared&&. - If the stop token of the receiver's environment can never request stop, which is the case for std::never_stop_token and for any token type that satisfies std::unstoppable_token, the operation never needs to complete with
set_stopped. A sender can then leaveset_stopped_t()out of its completion signatures for that environment, as the sender returned by thescheduleof a run_loop scheduler does. A receiver without a stop token in its environment gets a std::never_stop_token. unstoppable makes a sender run with such a token. - An operation that registers a stop callback with the stop token must destroy it before it completes, and must not use the token after it has completed. Since a stop request can come from another thread at any time, an operation that does work on another thread must make sure that only one of the completion functions is called, for example with an atomic variable that the stop callback and the code that completes normally both update.
- The operation state can be destroyed while
set_stoppedis still running, as soon as the receiver has what it needs. The call toset_stoppedshould therefore be the last thing the operation does with its own members. set_stoppedwas calledset_donein early proposals ofstd::execution.
Example
count_primes is a long computation that checks the stop token of its receiver and completes with set_stopped when stop is requested. When one of the senders run by when_all is stopped, the other one is asked to stop too, so the program does not spend minutes counting primes below 100'000'000.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <execution>
#include <print>
#include <stop_token>
#include <utility>
namespace ex = std::execution;
bool is_prime(int n)
{
for (int d = 2; d * d <= n; ++d)
if (n % d == 0)
return false;
return n > 1;
}
// A long computation that stops early if stop is requested
struct count_primes
{
using sender_concept = ex::sender_tag;
int limit;
template<class Self, class... Env>
static consteval auto get_completion_signatures()
{
return ex::completion_signatures<ex::set_value_t(int),
ex::set_stopped_t()>();
}
template<class Rcvr>
struct operation
{
using operation_state_concept = ex::operation_state_tag;
Rcvr rcvr;
int limit;
void start() & noexcept
{
auto token = std::get_stop_token(ex::get_env(rcvr));
int count = 0;
for (int n = 2; n < limit; ++n)
{
if (token.stop_requested())
{
// Nobody needs the result any more
ex::set_stopped(std::move(rcvr));
return;
}
count += is_prime(n);
}
ex::set_value(std::move(rcvr), count);
}
};
template<class Rcvr>
operation<Rcvr> connect(Rcvr rcvr) const
{
return {std::move(rcvr), limit};
}
};
int main()
{
auto [a] = std::this_thread::sync_wait(count_primes{100}).value();
std::println("Primes below 100: {}", a);
// The receiver's environment provides the stop token
std::inplace_stop_source source;
auto stop_env = ex::prop(std::get_stop_token, source.get_token());
auto stoppable = ex::write_env(count_primes{100'000'000}, stop_env);
source.request_stop();
// When one part of the work is stopped, when_all requests stop of the
// others, and sync_wait returns an empty optional
auto both = ex::when_all(stoppable, count_primes{100'000'000});
std::println("Completed with a value: {}",
std::this_thread::sync_wait(both).has_value());
// upon_stopped turns a stopped completion into a value completion
auto [b] = std::this_thread::sync_wait(stoppable | ex::upon_stopped([]
{
return -1;
})).value();
std::println("Result after a stop request: {}", b);
}
Output:
Primes below 100: 25
Completed with a value: false
Result after a stop request: -1
See also
(C++26) |
value completion function indicating successful completion (customization point object) |
(C++26) |
error completion function indicating that an error occurred during calculation or scheduling (customization point object) |
(C++26) |
specifies that a type is a receiver (concept) |
(C++26) |
returns a sender that immediately completes with the stopped signal (customization point object) |
(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 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) | |
(C++26)(C++26) |
asks a queryable object for the stop token of an asynchronous operation, or obtains the type of that token (customization point object) |