Documentation Home >> Headers >> <atomic> Header >> ATOMIC_VAR_INIT macro

The ATOMIC_VAR_INIT macro provides a means of initializing an atomic variable to a particular value.

#define ATOMIC_VAR_INIT(value) see description

The macro expands to a token sequence that can be used to initialize one of the standard atomic types with the specified value in an expression of the following form:

std::atomic<type> x = ATOMIC_VAR_INIT(val);

The specified value must be compatible with the non-atomic type corresponding to the atomic variable.


std::atomic<int> i = ATOMIC_VAR_INIT(42);
std::string s;
std::atomic<std::string*> p = ATOMIC_VAR_INIT(&s);

Such initialization is not atomic, and any access by another thread to the variable being initialized that does not happen-after the initialization is a data race and thus undefined behaviour.


#include <atomic>

See Also