std::stop_callback
來自 cppreference.com
定義於標頭檔案 <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