atomic_flag_test

atomic_flag_test (플래그 값 읽기)

std::atomic_flag의 현재 값을 원자적으로 읽어 반환하는 자유 함수예요. C++20부터 있어요.

출처: cppreference

본문

atomic_flag_test*object의 값을 원자적으로 읽고 반환해요.

bool atomic_flag_test( const std::atomic_flag* object ) noexcept;        // (2)

메모리 동기화 순서는 기본적으로 std::memory_order_seq_cst예요. _explicit 버전은 순서를 지정해요.

bool atomic_flag_test_explicit( const std::atomic_flag* object,
                                std::memory_order order ) noexcept;      // (4)

주의할 점은 orderstd::memory_order::release 또는 std::memory_order::acq_rel이면 동작이 미정의라는 거예요. 읽기(load) 연산이므로 해제 계열 순서는 의미가 없기 때문이에요.

std::atomic_flag flag = ATOMIC_FLAG_INIT;
if (atomic_flag_test(&flag)) {
    // 플래그가 set(true) 상태
}

값을 바꾸지 않고 상태만 확인하는 것이 핵심이에요. test_and_set이 값을 true로 바꾸며 이전 값을 돌려주는 것과 대비되죠.

더 알아보기 (Learn more)

cppreference