std::get_stop_token, std::stop_token_of_t
| Defined in header <execution>
|
||
struct get_stop_token_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr get_stop_token_t get_stop_token{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Env >
/* stoppable-token */ get_stop_token( const Env& env ) noexcept;
|
(3) | (since C++26) |
| Helper type |
||
template< class T >
using stop_token_of_t =
std::remove_cvref_t<decltype(get_stop_token(std::declval<T>()))>;
|
(4) | (since C++26) |
A query that asks an environment, usually the environment of a receiver, for the stop token through which stop can be requested for an asynchronous operation. An operation that supports cancellation obtains the token with get_stop_token(execution::get_env(rcvr)), and checks it with token.stop_requested(), or registers a callback on it with std::stop_callback_for_t, to stop early, usually completing with execution::set_stopped.
The caller decides which token an operation receives, through the environment of the receiver it connects. execution::when_all gives its children a token on which it requests stop when one of them fails or is stopped, or when stop is requested for when_all itself. execution::unstoppable gives its child a std::never_stop_token. A program provides its own token with execution::write_env(sndr, execution::prop(std::get_stop_token, token)), see write_env and prop. The query is forwarded through the environments of the receivers of sender adaptors, so nested operations receive the same token unless an adaptor on the way changes it.
get_stop_token(env) returns std::as_const(env).query(get_stop_token) if that expression is well-formed, and a default-constructed std::never_stop_token otherwise. The program is ill-formed if std::as_const(env).query(get_stop_token) can throw an exception, or if its type does not satisfy std::stoppable_token.T, usually an environment: the stop token type of a receiver of type Rcvr is std::stop_token_of_t<execution::env_of_t<Rcvr>>. An operation state uses it to name the type of its stop callback, std::stop_callback_for_t<std::stop_token_of_t<execution::env_of_t<Rcvr>>, Callback> (see std::stop_callback_for_t), and a sender uses it to decide its completion signatures, see below.Parameters
| env | - | the environment (or other queryable object) to query |
Return value
The stop token of env, or a default-constructed std::never_stop_token if it does not provide one.
Customization point objects
The name get_stop_token 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 get_stop_token_t. See CustomizationPointObject for details.
Notes
- Support for cancellation is optional, both for the operation and for the caller. An operation that does not query the stop token ignores stop requests. A caller that does not provide a stop token gets the default
std::never_stop_token, which models std::unstoppable_token, so an operation can checkstd::unstoppable_token<std::stop_token_of_t<execution::env_of_t<Rcvr>>>at compile time and leave out its cancellation code. The standard senders use this to leaveset_stoppedout of their completion signatures, for example the schedule senders of run_loop and task_scheduler. - std::this_thread::sync_wait does not provide a stop token, so stop cannot be requested for the work it waits for, unless the program provides a token with
write_env. - The token must stay valid until the operation completes or is destroyed: a receiver must provide a token that does, and an operation must not use the token, including through stop callbacks, after that. An operation usually destroys its stop callbacks before it calls a completion function, because the completion can end the lifetime of the stop source.
- Stop requests race with the normal completion of the work. An operation that is past the point where it can stop, or that checks the token only from time to time, can complete normally after stop was requested.
- Unlike most queries of the execution library,
get_stop_tokenandstop_token_of_tare in namespacestd, notstd::execution. - Earlier revisions of the proposal also had a form of
get_stop_tokenwithout arguments, which returned a sender equivalent toexecution::read_env(get_stop_token). It was removed before C++26 was published, andstop_token_of_twas calledstop_token_type_tuntil P2300R3. Older implementations and articles may show the earlier forms.
Example
A worker that runs until stop is requested for it. sync_wait provides no stop token, when_all requests stop when another child fails, and write_env provides the token of the program's own stop source.
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 <chrono>
#include <execution>
#include <print>
#include <stdexcept>
#include <stop_token>
#include <thread>
namespace ex = std::execution;
using namespace std::chrono_literals;
// Work that runs on the parallel scheduler until stop is requested for it
auto run_until_stopped()
{
return ex::starts_on(ex::get_parallel_scheduler(),
ex::read_env(std::get_stop_token) | ex::then([](auto token)
{
while (!token.stop_requested())
std::this_thread::sleep_for(1ms);
std::println("Worker: stop requested");
}));
}
int main()
{
// sync_wait provides no stop token, so the query returns never_stop_token
auto [token] =
std::this_thread::sync_wait(ex::read_env(std::get_stop_token)).value();
std::println("Under sync_wait, stop possible: {}", token.stop_possible());
// when_all requests stop for its other children when one of them fails
auto fail_later = ex::schedule(ex::get_parallel_scheduler())
| ex::then([]
{
std::this_thread::sleep_for(10ms);
throw std::runtime_error("Failed");
});
try
{
std::this_thread::sync_wait(
ex::when_all(run_until_stopped(), fail_later));
}
catch (const std::exception& e)
{
std::println("when_all: {}", e.what());
}
// A program provides its own stop token with write_env
std::inplace_stop_source source;
std::jthread stopper([&]
{
std::this_thread::sleep_for(10ms);
source.request_stop();
});
auto token_env = ex::prop(std::get_stop_token, source.get_token());
std::this_thread::sync_wait(ex::write_env(run_until_stopped(), token_env));
std::println("Done");
}
Output:
Under sync_wait, stop possible: false
Worker: stop requested
when_all: Failed
Worker: stop requested
Done
See also
(C++26) |
creates a sender that queries its receiver's associated environment (customization point object) |
(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) |
stopped completion function indicating that an operation ended before it could achieve success or failure (customization point object) |
(C++26) |
shields the provided sender from external cancellation requests, ensuring that the critical task runs uninterrupted to completion (customization point object) |
(C++26) |
specifies the basic interface of stop tokens which allows queries for stop requests and whether the stop request is possible (concept) |
(C++26) |
a stop token that references stop state of its associated std::inplace_stop_source object (class) |
(C++26) |
provides a stop token interface that a stop is never possible nor requested (class) |