First let us go through the definiton of few terms before understanding stop_token class::
std::jthread - C++ class for a joining and cooperative interruptible thread with stop_token helper.
std::stop_source - provides the means to issue a stop request, such as for std::jthread cancellation. A stop request made for one stop_source object is visible to all stop_sources.
Now,
The stop_token class provides the means to check if a stop request has been made or can be made, for its associated std::stop_source object.
It is essentially a thread-safe "view" of the associated stop-state.
A stop_token object is not generally constructed independently, but rather retrieved from astd::jthreadorstd::stop_source. This makes it share the same associated stop-state as thestd::jthreadorstd::stop_source.
The stop_token can also be passed to the constructor of std::stop_callback, such that the callback will be invoked if the stop_token's associated std::stop_source is requested to stop.
And stop_token can be passed to the interruptible waiting functions of std::condition_variable_any, to interrupt the condition variable's wait if stop is requested.
request_stop() returns whether the stop state was changed.
stop_callback has type member callback_type.
stop_callback constructor supports copying/conversion of callback.
stop_callback deduction guide now deduces to decayed type.
Few Member Functions are:
constructor()-constructs new stop_token object
destructor()-destructs stop_token object
operator= -assigns the stop_token object
Modifiers:
swap() - swaps two stop_token object
Observers
stop_requested - checks whether the associated stop-state has been requested to stop
stop_possible - checks whether associated stop-state can be requested to stop
Comments