Namespaces
Variants

std::jthread:: request_stop

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 request_stop ( ) noexcept ;
(C++20 이후)

아직 중지 요청이 이루어지지 않은 경우 내부 중지 상태에 중지 요청을 발행합니다.

결정은 원자적으로 이루어지며, 정지 요청이 있었을 경우 경쟁 조건을 피하기 위해 정지 상태가 원자적으로 업데이트됩니다. 예를 들면:

  • stop_requested ( ) stop_possible ( ) 는 동일한 공유 중지 상태를 가진 다른 std::stop_token std::stop_source 에서 동시에 호출될 수 있습니다.
  • request_stop ( ) 는 동일한 jthread 객체 또는 동일한 중지 상태와 연관된 다른 std::stop_source 객체에서 여러 스레드에 의해 동시에 호출될 수 있으며, 실제로 중지 요청을 수행하는 것은 하나만입니다.

그러나 Notes 섹션을 참조하십시오.

목차

매개변수

(없음)

반환값

true 이 호출이 중지 요청을 생성한 경우, 그렇지 않으면 false .

사후 조건

std::stop_token 으로부터 획득한 get_stop_token ( ) 또는 std::stop_source 으로부터 획득한 get_stop_source ( ) 의 경우, stop_requested ( ) true 입니다.

참고 사항

만약 request_stop ( ) 가 중지 요청을 발행한다면 (즉, true 를 반환한다면), 동일한 연관된 중지 상태에 등록된 모든 std::stop_callbacks 는 동기적으로, request_stop ( ) 가 발행된 동일한 스레드에서 호출됩니다. 콜백 호출이 예외를 통해 종료되면, std::terminate 가 호출됩니다.

중지 요청이 이미 이루어진 경우, 이 함수는 false 를 반환합니다. 그러나 다른 스레드나 std::stop_source 객체가 동일한 중지 상태에 대해 방금 (성공적으로) 중지를 요청한 경우, 해당 std::stop_callback 함수 호출을 아직 진행 중일 수 있다는 보장은 없습니다.

만약 request_stop ( ) 가 정지 요청을 발행한다면 (즉, true 를 반환한다면), 해당 jthread 의 내부 정지 상태와 연관된 std::stop_token 에 대해 인터럽트 가능 대기로 등록된 기본 타입 std::condition_variable_any 의 모든 조건 변수가 깨어나게 됩니다.

예제

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std::chrono_literals;
// 어느 스레드가 무엇을 출력했는지 빠르게 보여주는 헬퍼 함수
void print(auto txt)
{
    std::cout << std::this_thread::get_id() << ' ' << txt;
}
int main()
{
    // 잠자는 작업자 스레드
    std::jthread sleepy_worker(
        [](std::stop_token stoken)
        {
            for (int i = 10; i; --i)
            {
                std::this_thread::sleep_for(300ms);
                if (stoken.stop_requested())
                {
                    print("Sleepy worker is requested to stop\n");
                    return;
                }
                print("Sleepy worker goes back to sleep\n");
            }
        });
    // 대기 중인 작업자 스레드
    // 조건 변수는 정지 요청에 의해 깨어납니다.
    std::jthread waiting_worker(
        [](std::stop_token stoken)
        {
            std::mutex mutex;
            std::unique_lock lock(mutex);
            std::condition_variable_any().wait(lock, stoken, []{ return false; });
            print("Waiting worker is requested to stop\n");
            return;
        });
    // 스레드가 실행될 시간을 주기 위해 이 스레드를 잠듭니다
    std::this_thread::sleep_for(400ms);
    // std::jthread::request_stop()을 명시적으로 호출할 수 있습니다:
    print("Requesting stop of sleepy worker\n");
    sleepy_worker.request_stop();
    sleepy_worker.join();
    print("Sleepy worker joined\n");
    // 또는 RAII를 사용하여 자동으로:
    // waiting_worker의 소멸자가 request_stop()을 호출하고
    // 스레드를 자동으로 조인합니다.
}

가능한 출력:

140287602706176 Sleepy worker goes back to sleep
140287623300928 Requesting stop of sleepy worker
140287602706176 Sleepy worker is requested to stop
140287623300928 Sleepy worker joined
140287594313472 Waiting worker is requested to stop