名前空間
変種
操作

std::stop_callback

From cppreference.com
< cpp‎ | thread
 
 
並行性サポートライブラリ
スレッド
(C++11)
(C++20)
this_thread 名前空間
(C++11)
(C++11)
(C++11)
協調的なキャンセル
(C++20)
stop_callback
(C++20)
排他制御
(C++11)
汎用ロック管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件変数
(C++11)
セマフォ
ラッチとバリア
(C++20)
(C++20)
future
(C++11)
(C++11)
(C++11)
(C++11)
安全なメモリ解放 (Safe Reclamation)
(C++26)
ハザードポインタ
アトミック型
(C++11)
(C++20)
アトミック型の初期化
(C++11)(C++20で非推奨)
(C++11)(C++20で非推奨)
メモリオーダー
(C++11)(C++26で非推奨)
アトミック操作のためのフリー関数
アトミックフラグのためのフリー関数
 
 
ヘッダ <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_tokenstd::stop_source に対して request_stop() を正常に呼び出したのと同じスレッドで呼び出されるか、またはコンストラクタの登録前に既に停止が要求されていた場合は、stop_callback を構築するスレッドでコールバックが呼び出されます。

同じ std::stop_token に対して、同じスレッドまたは異なるスレッドから並行して複数の stop_callback を作成できます。それらが実行される順序は保証されませんが、同期的に呼び出されます。ただし、前述の通り、std::stop_token に対して停止が既に要求された後に構築された stop_callback は除きます。

コールバックの呼び出しが例外によって終了した場合、std::terminate が呼び出されます。

std::stop_callbackCopyConstructibleCopyAssignableMoveConstructible、および 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
English 日本語 中文(简体) 中文(繁體)