ATOMIC_VAR_INIT
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Concurrency support library
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
(C++26)
(C++20)
(C++26)
(C++20)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++26)
(C++11)
(C++11)
(C++17)
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++20)(C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++26)
(C++26)
(C++26)
(C++11)
(C++20)
(C++11)
(C++11)(deprecated in C++20)
ATOMIC_VAR_INIT
(C++11)(deprecated in C++20)
(C++11)
(C++11)
(C++11)(deprecated in C++26)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)
(C++26)(C++26)
(C++26)(C++26)
(C++11)
(C++20)(C++20)
(C++20)
(C++20)
(C++11)(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
Defined in header
<atomic>
#define ATOMIC_VAR_INIT(value) /* implementation-defined */
(since C++11) (deprecated in C++20)
Expands to an expression which can be used to initialize an std::atomic object that can be initialized from value
. If the atomic object has static storage duration, this initialization is constant initialization.
[edit] Notes
Accessing the variable during initialization from another thread, even through an atomic operation, is a data race (it may happen if the address is immediately passed to another thread with a std::memory_order_relaxed operation)
This macro is primarily provided for compatibility with C; it behaves the same as the constructor of std::atomic .
[edit] Example
Run this code
#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'; }
Output:
Initialized std::atomic<int> as: 1
[edit] See also
(C++11)(deprecated in C++20)
(function template) [edit]
C documentation for ATOMIC_VAR_INIT