Namespaces
Variants

std:: stop_callback

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
(C++20)
stop_callback
(C++20)
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
定義先ヘッダ <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 を構築するスレッドで呼び出されます。

同一の stop_callback に対して複数のコールバックが、同一または異なるスレッドから同時に作成されることがあります。実行順序に関する保証はありませんが、それらは同期的に呼び出されます。ただし、前述の通り、 std::stop_token に対して停止が既に要求された後に構築された stop_callback は例外です。

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

std::stop_callback CopyConstructible CopyAssignable MoveConstructible MoveAssignable のいずれでもありません。

テンプレートパラメータ Callback 型は invocable かつ destructible でなければなりません。戻り値は無視されます。

目次

メンバー型

定義
callback_type Callback

メンバー関数

新しい stop_callback オブジェクトを構築する
(公開メンバ関数)
stop_callback オブジェクトを破棄する
(公開メンバ関数)
operator=
[deleted]
stop_callback は代入不可
(公開メンバ関数)

推論ガイド

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
using namespace std::chrono_literals;
// アトミックなstd::coutストリーミングのためのヘルパークラス
class Writer
{
    std::ostringstream buffer;
public:
    ~Writer()
    {
        std::cout << buffer.str();
    }
    Writer& operator<<(auto input)
    {
        buffer << input;
        return *this;
    }
};
int main()
{
    // ワーカースレッド
    // 停止要求があるまで待機する
    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(); });
    });
    // ワーカースレッドにストップコールバックを登録
    std::stop_callback callback(worker.get_stop_token(), []
    {
        Writer() << "Stop callback executed by thread: "
            << std::this_thread::get_id() << '\n';
    });
    // stop_callbackオブジェクトは実行を防ぐために早期に破棄可能
    {
        std::stop_callback scoped_callback(worker.get_stop_token(), []
        {
            // これは実行されない
            Writer() << "Scoped stop callback executed by thread: "
                << std::this_thread::get_id() << '\n';
        });
    }
    // どのスレッドがいつstop_callbackを実行するかを実証
    // ストッパー関数を定義
    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';
    };
    // 複数のスレッドでワーカースレッドの停止を競わせる
    std::jthread stopper1(stopper_func);
    std::jthread stopper2(stopper_func);
    stopper1.join();
    stopper2.join();
    // 停止が既に要求された後では、
    // 新しいstop_callbackは即座に実行される
    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