ATOMIC_VAR_INIT

ATOMIC_VAR_INIT

Defined in header <atomic>
#define ATOMIC_VAR_INIT(value) /* implementation-defined */

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.

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.

Example

#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

See also

(C++11)
non-atomic initialization of a default-constructed atomic object
(function template)
constructs an atomic object
(public member function of std::atomic)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/atomic/ATOMIC_VAR_INIT

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部