bsl::stop_token

This class is a mechanism for observing cancellation requests. An object of this class either has (possibly shared) ownership of a stop state and can be used to observe whether a cancellation request has been made on that stop state, or does not own a stop state. A stop_token cannot be used to make a cancellation request.

Synopsis

Declared in <bslstl_stoptoken.h>

class stop_token;

Type Aliases

NameDescription
callback_type Alias for stop_callback parameterized by t_CALLBACK.

Member Functions

NameDescription
stop_token [constructor]Constructors
~stop_token [destructor]Destroy this object.
operator= Assignment operators
stop_possible Return true if *this refers to a stop state, and either a stop was already requested on that stop state or there is at least one stop_source object that refers to that stop state (implying that a stop could still be requested using the request_stop function), and false otherwise. A call to stop_possible that is potentially concurrent with a call to stop_requested or stop_possible does not cause a data race.
stop_requested Return true if *this refers to a stop state on which request_stop has been called, and false otherwise. If this function returns true, then the successful call to request_stop synchronizes with this call. A call to stop_requested that is potentially concurrent with a call to stop_requested or stop_possible does not cause a data race.
swap Set *this to refer to the stop state (or lack thereof) that the specified other referred to, and vice versa. Equivalent to swap(*this, other).

Friends

NameDescription
bsl::swapSet lhs to refer to the stop state (or lack thereof) that rhs referred to, and vice versa. Implementation note: this function is required by the standard to be a hidden friend ([hidden.friends], [stoptoken.general]).
bsl::operator==Return true if the specified lhs and rhs refer to the same stop state, or if neither refers to a stop state; false otherwise. Implementation note: this function is required by the standard to be a hidden friend ([hidden.friends], [stoptoken.general]).
bsl::stop_callbackThis class holds an object or reference of type t_CALLBACK and, when constructed using a stop_token that owns a stop state, schedules the held object or reference to be executed by the thread that requests cancellation on that stop state (if any). However, if cancellation was already requested before the stop_callback was constructed, the constructor invokes the callback immediately. If there is no stop state, or request_stop is never called for the stop state, then the callback is not invoked. stop_callback stores its callback within its own footprint, and thus never requires memory allocation; however, stop_callback<t_CALLBACK> is an allocator-aware class, if t_CALLBACK is an allocator-aware class, and any supplied allocator will then be passed to the constructor of t_CALLBACK.
bsl::stop_sourceThis class is a mechanism for making and observing cancellation requests. An object of this class may have (possibly shared) ownership of a stop state, in which case it can be used to make a cancellation request or observe whether a cancellation request has been made on the owned stop state; it is also possible for a stop_source object to not own a stop state. Due to its shared ownership semantics, it is safe to pass a copy of a stop_source object to a callback that might outlive the original stop_source object; however, a callback that should only be able to observe a cancellation request, without being able to request cancellation itself, should instead be passed a stop_token, which can be created by calling stop_source::get_token.