Namespaces
Variants

ATOMIC_VAR_INIT

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
ATOMIC_VAR_INIT
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
헤더 파일에 정의됨 <atomic>
#define ATOMIC_VAR_INIT(value) /* 구현 정의 */
(C++11부터)
(C++20부터 사용 중단됨)

value 로부터 초기화될 수 있는 std::atomic 객체를 초기화하는 데 사용할 수 있는 표현식으로 확장됩니다. 원자 객체가 정적 저장 기간을 가지는 경우, 이 초기화는 상수 초기화 입니다.

참고 사항

다른 스레드에서 초기화 중인 변수에 접근하는 것은(원자 연산을 통하더라도) 데이터 경쟁입니다(주소가 std::memory_order_relaxed 연산과 함께 즉시 다른 스레드로 전달될 경우 발생할 수 있음)

이 매크로는 주로 C와의 호환성을 위해 제공됩니다; 이것은 std::atomic 의 생성자와 동일하게 동작합니다.

예제

#include <atomic>
#include <iostream>
int main()
{
    std::atomic<int> a = ATOMIC_VAR_INIT(1);
    // std::atomic<int> a(1);   // C++-only alternative
    std::cout << "Initialized std::atomic<int> as: " << a << '\n';
}

출력:

Initialized std::atomic<int> as: 1

참고 항목

(C++11) (deprecated in C++20)
기본 생성된 atomic 객체의 비-원자적 초기화
(함수 템플릿)
atomic 객체를 생성함
( std::atomic<T> 의 public 멤버 함수)
C documentation for ATOMIC_VAR_INIT