atomic_atomic_flag_wait

atomic_atomic_flag_wait (원자적 플래그 대기)

std::atomic_flag_waitstd::atomic_flag의 값이 기대값과 다를 때까지 블록하며 기다려요.

출처: cppreference

본문

std::atomic_flag_waitstd::atomic_flag_wait_explicit<atomic> 헤더에 정의된 자유 함수예요. C++20부터 사용 가능해요.

void atomic_flag_wait( const std::atomic_flag* flag,
                       bool old ) noexcept;

void atomic_flag_wait_explicit( const std::atomic_flag* flag,
                                bool old,
                                std::memory_order order ) noexcept;

flag->load() 값이 old와 같을 때까지 스레드를 블록해요. (스퍼리어스 웨이크업이 허용되므로, 깨어난 후 다시 값을 확인해야 해요.) 다른 스레드가 atomic_flag_notify_one이나 atomic_flag_notify_all로 알려주면 깨어나요.

예제 (Example)

#include <atomic>
#include <thread>

std::atomic_flag flag;
int main()
{
    std::thread([&]{ std::atomic_flag_test_and_set(&flag);
                     std::atomic_flag_notify_one(&flag); }).detach();
    std::atomic_flag_wait(&flag, false);
}

더 알아보기 (Learn more)

cppreference