Namespaces
Variants

std::jthread:: join

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

현재 스레드를 * this 로 식별되는 스레드의 실행이 완료될 때까지 차단합니다.

* this 로 식별된 스레드의 완료는 동기화됩니다 join() 의 해당 성공적 반환과 함께.

* this 자체에는 동기화가 수행되지 않습니다. 여러 스레드에서 동일한 jthread 객체의 join ( ) 를 동시에 호출하는 것은 데이터 경쟁을 구성하며, 이는 정의되지 않은 행동을 초래합니다.

목차

매개변수

(없음)

반환값

(없음)

사후 조건

joinable() false 입니다.

예외

std::system_error 오류가 발생하는 경우.

오류 조건

예제

#include <chrono>
#include <iostream>
#include <thread>
void foo()
{
    // 비용이 큰 연산 시뮬레이션
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
    // 비용이 큰 연산 시뮬레이션
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
    std::cout << "첫 번째 헬퍼 시작 중...\n";
    std::jthread helper1(foo);
    std::cout << "두 번째 헬퍼 시작 중...\n";
    std::jthread helper2(bar);
    std::cout << "헬퍼들이 완료되기를 기다리는 중..." << std::endl;
    helper1.join();
    helper2.join();
    std::cout << "완료!\n";
}

출력:

첫 번째 헬퍼 시작 중...
두 번째 헬퍼 시작 중...
헬퍼들이 완료되기를 기다리는 중...
완료!

참고문헌

  • 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]

참고 항목

스레드가 스레드 핸들로부터 독립적으로 실행되도록 허용합니다
(public member function)
스레드가 조인 가능한지 확인합니다. 즉 병렬 컨텍스트에서 실행 중일 가능성이 있는지 확인합니다
(public member function)
C documentation for thrd_join