Namespaces
Variants

std::jthread:: detach

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 detach ( ) ;
(C++20부터)

jthread 객체로부터 실행 스레드를 분리하여 실행이 독립적으로 계속되도록 허용합니다. 할당된 모든 리소스는 스레드가 종료되면 해제됩니다.

detach 를 호출한 후 * this 는 더 이상 어떤 스레드도 소유하지 않습니다.

목차

매개변수

(없음)

반환값

(없음)

사후 조건

joinable false 입니다.

예외

std::system_error 만약 joinable ( ) == false 이거나 오류가 발생하는 경우.

예제

#include <chrono>
#include <iostream>
#include <thread>
void independentThread() 
{
    std::cout << "Starting concurrent thread.\n";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Exiting concurrent thread.\n";
}
void threadCaller() 
{
    std::cout << "Starting thread caller.\n";
    std::jthread t(independentThread);
    t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Exiting thread caller.\n";
}
int main() 
{
    threadCaller();
    std::this_thread::sleep_for(std::chrono::seconds(5));
}

가능한 출력:

Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.

참고문헌

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