Namespaces
Variants

std::stop_token:: stop_possible

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
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
bool stop_possible ( ) const noexcept ;
(C++20以降)

stop_token オブジェクトが関連する停止状態を持っているかどうか、およびその状態に対して既に停止要求が行われているか、あるいは関連する std::stop_source オブジェクトが存在するかどうかをチェックします。

デフォルト構築された stop_token は関連する停止状態を持たないため、停止することはできません。また、 std::stop_source オブジェクトが存在しない関連停止状態についても、そのような要求が既に行われていない場合、停止することはできません。

目次

パラメータ

(なし)

戻り値

false 関連する停止状態を持たない、または停止要求がまだなく、関連する std::stop_source オブジェクトが存在しない場合の stop_token オブジェクトの状態; true それ以外の場合。

注記

stop_token オブジェクトが関連する停止状態を持ち、停止要求が既に行われている場合、この関数は依然として true を返します。

stop_token オブジェクトが std::jthread から関連する停止状態を持っている場合—例えば、 stop_token std::jthread オブジェクトに対して get_stop_token ( ) を呼び出すことで取得された場合—この関数は常に true を返します。 std::jthread は、スレッドの呼び出し関数がそれをチェックしない場合でも、常に内部の std::stop_source オブジェクトを持っています。

#include <chrono>
#include <condition_variable>
#include <format>
#include <iostream>
#include <mutex>
#include <string_view>
#include <thread>
using namespace std::chrono_literals;
int main()
{
    std::cout << std::boolalpha;
    auto print = [](std::string_view name, const std::stop_token& token)
    {
        std::cout << std::format("{}: stop_possible = {:s}, stop_requested = {:s}\n", 
            name, token.stop_possible(), token.stop_requested()
        );
    };
    // 停止要求を監視するワーカースレッド
    auto stop_worker = std::jthread([](std::stop_token stoken)
    {
        for (int i = 10; i; --i)
        {
            std::this_thread::sleep_for(300ms);
            if (stoken.stop_requested())
            {
                std::cout << "  眠たいワーカーは停止を要求されました\n";
                return;
            }
            std::cout << "  眠たいワーカーは再び眠ります\n";
        }
    });
    // 完了時のみ停止するワーカースレッド
    auto inf_worker = std::jthread([]()
    {
        for (int i = 5; i; --i)
        {
            std::this_thread::sleep_for(300ms);
            std::cout << "  好きなだけ実行します\n";
        }
    });
    std::stop_token def_token;
    std::stop_token stop_token = stop_worker.get_stop_token();
    std::stop_token inf_token = inf_worker.get_stop_token();
    print("def_token ", def_token);
    print("stop_token", stop_token);
    print("inf_token ", inf_token);
    std::cout << "\nstop_workerの要求と結合:\n";
    stop_worker.request_stop();
    stop_worker.join();
    std::cout << "\ninf_workerの要求と結合:\n";
    inf_worker.request_stop();
    inf_worker.join();
    std::cout << '\n';
    print("def_token ", def_token);
    print("stop_token", stop_token);
    print("inf_token ", inf_token);
}

出力例:

def_token : stop_possible = false, stop_requested = false
stop_token: stop_possible = true, stop_requested = false
inf_token : stop_possible = true, stop_requested = false
Request and join stop_worker:
  Run as long as we want
  Sleepy worker is requested to stop
Request and join inf_worker:
  Run as long as we want
  Run as long as we want
  Run as long as we want
  Run as long as we want
def_token : stop_possible = false, stop_requested = false
stop_token: stop_possible = true, stop_requested = true
inf_token : stop_possible = true, stop_requested = true