atomic_atomic_flag

atomic_atomic_flag (원자적 플래그 타입)

std::atomic_flag는 불리언 원자적 타입이에요. test_and_setclear 연산만 지원하며, 스핀락 같은 단순 동기화에 써요.

출처: cppreference

본문

std::atomic_flag<atomic> 헤더에 정의된 원자적 플래그 타입이에요. 다른 원자적 타입과 달리 load/store가 없고 test_and_set(설정)과 clear(해제)만 지원해요. 복사나 이동은 불가능해요.

class atomic_flag;

멤버 함수

  • test — 현재 값을 확인해요 (C++20).
  • test_and_set — 플래그를 설정하고 이전 값을 반환해요.
  • clear — 플래그를 해제해요.
  • wait / notify_one / notify_all — 플래그에 대한 대기/알림 (C++20).

초기화

std::atomic_flag f = ATOMIC_FLAG_INIT;  // clear 상태로 초기화

예제 (Example)

#include <atomic>
#include <thread>

std::atomic_flag lock = ATOMIC_FLAG_INIT;

void spin_lock() { while (lock.test_and_set()); }
void spin_unlock() { lock.clear(); }

int main()
{
    spin_lock();
    // 임계 구역
    spin_unlock();
}

더 알아보기 (Learn more)

cppreference