std::stop_callback
来自 cppreference.cn
定义于头文件 <stop_token> |
||
template< class Callback > class stop_callback; |
(C++20 起) | |
stop_callback
类模板提供了一个 RAII 对象类型,它为关联的 std::stop_token 对象注册一个回调函数,以便当 std::stop_token 的关联 std::stop_source 请求停止时,将调用该回调函数。
通过 stop_callback
的构造函数注册的回调函数,要么在成功调用 std::stop_source(与 stop_callback
的关联 std::stop_token 相关联)的 request_stop() 的线程中调用;如果在此构造函数注册之前已请求停止,则回调将在构造 stop_callback
的线程中调用。
可以为同一个 std::stop_token 从相同或不同的线程并发创建多个 stop_callback
。不保证它们的执行顺序,但它们将同步调用;除了在已为 std::stop_token 请求停止后构造的 stop_callback
(s),如前所述。
如果回调调用通过异常退出,则调用 std::terminate。
std::stop_callback
既不是 可复制构造 (CopyConstructible),也不是 可复制赋值 (CopyAssignable),也不是 可移动构造 (MoveConstructible),也不是 可移动赋值 (MoveAssignable)。
模板参数 Callback
类型必须同时是 可调用 (invocable)
和 可析构 (destructible)
的。任何返回值都将被忽略。
目录 |
[编辑] 成员类型
类型 | 定义 |
callback_type
|
Callback
|
[编辑] 成员函数
构造新的 stop_callback 对象(public 成员函数) | |
析构 stop_callback 对象(public 成员函数) | |
operator= [已删除] |
stop_callback 不可赋值(public 成员函数) |
[编辑] 推导指南
[编辑] 示例
运行此代码
#include <chrono> #include <condition_variable> #include <iostream> #include <mutex> #include <sstream> #include <thread> using namespace std::chrono_literals; // Use a helper class for atomic std::cout streaming. class Writer { std::ostringstream buffer; public: ~Writer() { std::cout << buffer.str(); } Writer& operator<<(auto input) { buffer << input; return *this; } }; int main() { // A worker thread. // It will wait until it is requested to stop. std::jthread worker([] (std::stop_token stoken) { Writer() << "Worker thread's id: " << std::this_thread::get_id() << '\n'; std::mutex mutex; std::unique_lock lock(mutex); std::condition_variable_any().wait(lock, stoken, [&stoken] { return stoken.stop_requested(); }); }); // Register a stop callback on the worker thread. std::stop_callback callback(worker.get_stop_token(), [] { Writer() << "Stop callback executed by thread: " << std::this_thread::get_id() << '\n'; }); // Stop_callback objects can be destroyed prematurely to prevent execution. { std::stop_callback scoped_callback(worker.get_stop_token(), [] { // This will not be executed. Writer() << "Scoped stop callback executed by thread: " << std::this_thread::get_id() << '\n'; }); } // Demonstrate which thread executes the stop_callback and when. // Define a stopper function. auto stopper_func = [&worker] { if (worker.request_stop()) Writer() << "Stop request executed by thread: " << std::this_thread::get_id() << '\n'; else Writer() << "Stop request not executed by thread: " << std::this_thread::get_id() << '\n'; }; // Let multiple threads compete for stopping the worker thread. std::jthread stopper1(stopper_func); std::jthread stopper2(stopper_func); stopper1.join(); stopper2.join(); // After a stop has already been requested, // a new stop_callback executes immediately. Writer() << "Main thread: " << std::this_thread::get_id() << '\n'; std::stop_callback callback_after_stop(worker.get_stop_token(), [] { Writer() << "Stop callback executed by thread: " << std::this_thread::get_id() << '\n'; }); }
可能的输出
Worker thread's id: 140460265039616 Stop callback executed by thread: 140460256646912 Stop request executed by thread: 140460256646912 Stop request not executed by thread: 140460248254208 Main thread: 140460265043776 Stop callback executed by thread: 140460265043776