atomic_flag_wait
atomic_flag_wait (플래그가 변할 때까지 대기)
std::atomic_flag의 값이 기대값과 같을 때 블록했다가 통지되면 깨어나는 원자 대기 연산이에요. C++20부터 있어요.
출처: cppreference
본문
atomic_flag_wait는 원자 대기 연산을 수행해요.
void atomic_flag_wait( const atomic_flag* object, bool old ) noexcept; // (1)
object->test(std::memory_order_seq_cst)(또는 order) 값을 old와 비교해서, 같으면 *object가 std::atomic_flag::notify_one()이나 notify_all()로 통지될 때까지(또는 스퓨리어스하게 깨어날 때까지) 블록해요. 값이 다를 때까지 이 과정을 반복해요.
void atomic_flag_wait_explicit( const atomic_flag* object,
bool old, std::memory_order order ) noexcept; // (3)
_explicit 버전은 메모리 순서를 직접 지정해요.
- 매개변수
object: 대기할 atomic_flag 포인터. - 매개변수
old: 비교할 기대값. - 반환 값: 없음.
전형적인 사용은 "플래그가 어떤 값이 될 때까지 기다리기"예요. 대기자는 CPU를 소모하지 않고 블록했다가 통지로 깨어나므로 폴링보다 효율적이에요.
// 생산자가 notify_all()로 신호를 보내면 대기자가 깨어남
std::atomic_flag flag;
// 초기화 후...
atomic_flag_wait(&flag, /*old=*/false);
wait/notify 짝으로 스핀 대신 효율적인 이벤트 신호를 만들 수 있어요.