Daily bit(e) of C++ | std::stop_source, std::stop_token, std::stop_callback
Daily bit(e) of C++ ♻️10, The C++20 standard tools for issuing stop requests: std::stop_source, std::stop_token, std::stop_callback.
C++20 introduced a standard tool for issuing stop requests: std::stop_source, std::stop_token, and std::stop_callback.
A std::stop_source can be used to request a stop, which can then be observed thread-safe through the associated std::stop_token. Any callbacks registered through a std::stop_callback will also be run when a stop is requested.
The std::stop_source and std::stop_token are also integrated with std::jthread and std::condition_variable_any. If the callable passed to a std::jthread accepts a stop token, a stop will be automatically requested during std::jthread destruction before joining the thread.