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 객체에 연결된 중단 상태(stop-state)가 존재하는지, 그리고 해당 상태에 이미 중단 요청이 있었거나 연결된 std::stop_source 객체가 있는지 확인합니다.

기본 생성된 stop_token 은 연관된 중지 상태를 가지지 않으므로 중지될 수 없습니다; std::stop_source 객체가 존재하지 않는 연관된 중지 상태 또한, 이미 그러한 요청이 이루어지지 않은 경우 중지될 수 없습니다.

목차

매개변수

(없음)

반환값

false 만약 stop_token 객체가 연관된 중단 상태를 가지고 있지 않거나, 아직 중단 요청을 받지 않았으며 연관된 std::stop_source 객체가 없는 경우; 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 << "  Sleepy worker is requested to stop\n";
                return;
            }
            std::cout << "  Sleepy worker goes back to sleep\n";
        }
    });
    // 완료될 때만 정지하는 작업자 스레드
    auto inf_worker = std::jthread([]()
    {
        for (int i = 5; i; --i)
        {
            std::this_thread::sleep_for(300ms);
            std::cout << "  Run as long as we want\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 << "\nRequest and join stop_worker:\n";
    stop_worker.request_stop();
    stop_worker.join();
    std::cout << "\nRequest and join inf_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