std::stop_callback
| ヘッダ <stop_token> で定義 |
||
| template< class Callback > class stop_callback; |
(C++20以降) | |
stop_callback クラステンプレートは、関連する std::stop_token オブジェクトにコールバック関数を登録する RAII オブジェクト型を提供します。このコールバック関数は、std::stop_token の関連する std::stop_source が停止を要求されたときに呼び出されます。
stop_callback のコンストラクタを介して登録されたコールバック関数は、stop_callback の関連する std::stop_token の std::stop_source に対して request_stop() を正常に呼び出したのと同じスレッドで呼び出されるか、またはコンストラクタの登録前に既に停止が要求されていた場合は、stop_callback を構築するスレッドでコールバックが呼び出されます。
同じ std::stop_token に対して、同じスレッドまたは異なるスレッドから並行して複数の stop_callback を作成できます。それらが実行される順序は保証されませんが、同期的に呼び出されます。ただし、前述の通り、std::stop_token に対して停止が既に要求された後に構築された stop_callback は除きます。
コールバックの呼び出しが例外によって終了した場合、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