stop_callback - DryPerspective/Cpp17_jthread GitHub Wiki

Stop Callback

Class template dp::stop_callback allows the user to register callbacks which will be called when a stop request is made. It follows the pattern of std::stop_callback and allows RAII-style management of the callback - the constructor registers the callback to be executed on a stop request; and the destructor deregisters the callback from the list of callbacks to be called on a successful stop request. If a callback is registered before a stop request is made and remains registered until the stop request, it will be executed in the context of the thread which requested the stop. If the user attempts to register a callback after a stop is requested, it will be immediately executed in the context of the thread in which the callback is being constructed. If a callback is deregistered before a stop is requested, it will not be executed.

All callbacks must be invocable with no arguments. The return value of any callback is ignored.

Interface

dp::stop_callback is not copyable or movable. It's only noteworthy interface is its constructor, which accepts a dp::stop_token and a callable and registers the callable as a callback on the stop state associated with the stop token; and its destructor, which deregisters the callback if applicable.