notify_all_at_thread_exit — std::notify_all_at_thread_exit

notify_all_at_thread_exit — std::notify_all_at_thread_exit

std::notify_all_at_thread_exit주어진 스레드가 완전히 끝났을 때(스레드 지역 객체 파괴까지 포함해) 다른 스레드에 알리는 메커니즘을 제공해요. C++11에서 도입됐어요. <condition_variable> 헤더에 있어요.

출처: cppreference

본문

// <condition_variable> 헤더, C++11
void notify_all_at_thread_exit( std::condition_variable& cond,
                                std::unique_lock<std::mutex> lk );

스레드가 종료될 때(모든 스레드 로컬 객체가 파괴된 후) cond에 대해 notify_all을 호출해, 대기 중인 스레드를 깨워요.

사용 예

#include <condition_variable>
#include <mutex>
#include <thread>

std::condition_variable cv;
std::mutex m;
bool done = false;

int main() {
    std::thread t([] {
        std::unique_lock<std::mutex> lk(m);
        done = true;
        // 스레드 종료 시 (thread-local 파괴 후) cv에 notify_all
        std::notify_all_at_thread_exit(cv, std::move(lk));
    });

    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, []{ return done; });   // t의 thread-local이 정리된 뒤 깨어남
    t.join();
}

특징

  • notify_all스레드 자체가 완전히 종료된 후에 수행되도록 함 — std::thread join() 없이도 "스레드가 다 끝났다"는 것을 알림.
  • unique_lock은 이미 잠긴 상태로 넘겨야 해요.
  • 스레드 종료 시 자동으로 호출되는 확실한 알림 메커니즘이에요.

notify_all_at_thread_exit은 스레드가 자신의 스레드 로컬 정리를 모두 마친 시점을 다른 스레드에 안전하게 알릴 때 쓰여요.

더 알아보기 (Learn more)

cppreference