Namespaces
Variants

std::jthread:: joinable

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

std::jthread 객체가 활성 실행 스레드를 식별하는지 확인합니다. 구체적으로, true 를 반환하는 조건은 get_id ( ) ! = std :: jthread :: id ( ) 입니다. 따라서 기본 생성된 jthread 는 조인 가능하지 않습니다.

실행 코드를 완료했지만 아직 조인되지 않은 스레드는 여전히 실행 중인 활성 스레드로 간주되므로 조인 가능한 상태입니다.

목차

매개변수

(없음)

반환값

true 만약 std::jthread 객체가 실행 중인 활성 스레드를 식별하는 경우, false 그렇지 않은 경우.

예제

#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
void foo()
{
    std::this_thread::sleep_for(500ms);
}
int main()
{
    std::cout << std::boolalpha;
    std::jthread t;
    std::cout << "before starting, joinable: " << t.joinable() << '\n';
    t = std::jthread{foo};
    std::cout << "after starting, joinable: " << t.joinable() << '\n';
    t.join();
    std::cout << "after joining, joinable: " << t.joinable() << '\n';
    t = std::jthread{foo};
    t.detach();
    std::cout << "after detaching, joinable: " << t.joinable() << '\n';
}

출력:

before starting, joinable: false
after starting, joinable: true
after joining, joinable: false
after detaching, joinable: false

참고문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 33.4.4.3 멤버 [thread.jthread.mem]
  • C++20 표준(ISO/IEC 14882:2020):
  • 32.4.3.2 멤버 [thread.jthread.mem]

참고 항목

스레드의 id 를 반환합니다
(public member function)
스레드 실행이 완료될 때까지 대기합니다
(public member function)
스레드가 스레드 핸들로부터 독립적으로 실행되도록 허용합니다
(public member function)