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.
Compiler Explorer link: https://compiler-explorer.com/z/3s5b85MzY