在线文档教程
C++

std::atomic

std::atomic

Defined in header
template< class T > struct atomic;(1)(since C++11)
template<> struct atomic<Integral>;(2)(since C++11)
template<> struct atomic<bool>;(3)(since C++11)
template< class T > struct atomic<T*>;(4)(since C++11)

Each instantiation and full specialization of the std::atomic template defines an atomic type. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races).

In addition, accesses to atomic objects may establish inter-thread synchronization and order non-atomic memory accesses as specified by std::memory_order.

std::atomic may be instantiated with any TriviallyCopyable type T. std::atomic is neither copyable nor movable.

The standard library provides specializations of the std::atomic template for the following types:

1) One full specialization for the type bool and its typedef name is defined that is treated as a non-specialized std::atomic<T> except that it has standard layout, trivial default constructor, trivial destructors, and supports aggregate initialization syntax:

Typedef nameFull specialization
std::atomic_boolstd::atomic<bool>

2) Full specializations and typedefs for integral types, as follows:

Typedef nameFull specialization
std::atomic_charstd::atomic<char>
std::atomic_scharstd::atomic<signed char>
std::atomic_ucharstd::atomic<unsigned char>
std::atomic_shortstd::atomic<short>
std::atomic_ushortstd::atomic<unsigned short>
std::atomic_intstd::atomic<int>
std::atomic_uintstd::atomic<unsigned int>
std::atomic_longstd::atomic<long>
std::atomic_ulongstd::atomic<unsigned long>
std::atomic_llongstd::atomic<long long>
std::atomic_ullongstd::atomic<unsigned long long>
std::atomic_char16_tstd::atomic<char16_t>
std::atomic_char32_tstd::atomic<char32_t>
std::atomic_wchar_tstd::atomic<wchar_t>
std::atomic_int8_tstd::atomic<std::int8_t>
std::atomic_uint8_tstd::atomic<std::uint8_t>
std::atomic_int16_tstd::atomic<std::int16_t>
std::atomic_uint16_tstd::atomic<std::uint16_t>
std::atomic_int32_tstd::atomic<std::int32_t>
std::atomic_uint32_tstd::atomic<std::uint32_t>
std::atomic_int64_tstd::atomic<std::int64_t>
std::atomic_uint64_tstd::atomic<std::uint64_t>
std::atomic_int_least8_tstd::atomic<std::int_least8_t>
std::atomic_uint_least8_tstd::atomic<std::uint_least8_t>
std::atomic_int_least16_tstd::atomic<std::int_least16_t>
std::atomic_uint_least16_tstd::atomic<std::uint_least16_t>
std::atomic_int_least32_tstd::atomic<std::int_least32_t>
std::atomic_uint_least32_tstd::atomic<std::uint_least32_t>
std::atomic_int_least64_tstd::atomic<std::int_least64_t>
std::atomic_uint_least64_tstd::atomic<std::uint_least64_t>
std::atomic_int_fast8_tstd::atomic<std::int_fast8_t>
std::atomic_uint_fast8_tstd::atomic<std::uint_fast8_t>
std::atomic_int_fast16_tstd::atomic<std::int_fast16_t>
std::atomic_uint_fast16_tstd::atomic<std::uint_fast16_t>
std::atomic_int_fast32_tstd::atomic<std::int_fast32_t>
std::atomic_uint_fast32_tstd::atomic<std::uint_fast32_t>
std::atomic_int_fast64_tstd::atomic<std::int_fast64_t>
std::atomic_uint_fast64_tstd::atomic<std::uint_fast64_t>
std::atomic_intptr_tstd::atomic<std::intptr_t>
std::atomic_uintptr_tstd::atomic<std::uintptr_t>
std::atomic_size_tstd::atomic<std::size_t>
std::atomic_ptrdiff_tstd::atomic<std::ptrdiff_t>
std::atomic_intmax_tstd::atomic<std::intmax_t>
std::atomic_uintmax_tstd::atomic<std::uintmax_t>

Note: std::atomic_intN_t, std::atomic_uintN_t, std::atomic_intptr_t, and atomic_uintptr_t are defined if and only if std::intN_t, std::uintN_t, std::intptr_t, and std::uintptr_t are defined, respectively.

These specializations have standard layout, trivial default constructors, and trivial destructors. They support aggregate initialization syntax. Besides the operations provided for all atomic types, these specializations have additional atomic operations appropriate to integer types such as fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor.

3) Partial specializations std::atomic<T*> for all pointer types. These specializations have standard layout, trivial default constructors, and trivial destructors. They support aggregate initialization syntax. Besides the operations provided for all atomic types, these specializations additionally support atomic arithmetic operations appropriate to pointer types, such as fetch_add, fetch_sub.

Member functions

(constructor)constructs an atomic object (public member function)
operator=stores a value into an atomic object (public member function)
is_lock_freechecks if the atomic object is lock-free (public member function)
storeatomically replaces the value of the atomic object with a non-atomic argument (public member function)
loadatomically obtains the value of the atomic object (public member function)
operator Tloads a value from an atomic object (public member function)
exchangeatomically replaces the value of the atomic object and obtains the value held previously (public member function)
compare_exchange_weakcompare_exchange_strongatomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not (public member function)

| Constants |

| is_always_lock_free static | indicates that the type is always lock-free (public static member constant) |

Specialized member functions

fetch_addatomically adds the argument to the value stored in the atomic object and obtains the value held previously (public member function)
fetch_subatomically subtracts the argument from the value stored in the atomic object and obtains the value held previously (public member function)
fetch_andatomically performs bitwise AND between the argument and the value of the atomic object and obtains the value held previously (public member function)
fetch_oratomically performs bitwise OR between the argument and the value of the atomic object and obtains the value held previously (public member function)
fetch_xoratomically performs bitwise XOR between the argument and the value of the atomic object and obtains the value held previously (public member function)
operator++operator++(int)operator--operator--(int)increments or decrements the atomic value by one (public member function)
operator+=operator-=operator&=operator|=operator^=adds, subtracts, or performs bitwise AND, OR, XOR with the atomic value (public member function)

Notes

There are non-member function template equivalents for all member functions of std::atomic. Those non-member functions may be additionally overloaded for types that are not specializations of std::atomic, but are able to guarantee atomicity. The only such type in the standard library is std::shared_ptr<T>.

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2441c++11added specializations for the (optional) fixed width integer types

See also

atomic_flag (C++11)the lock-free boolean atomic type (class)

References

  • C++11 standard (ISO/IEC 14882:2011):

© cppreference.com

Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.

http://en.cppreference.com/w/cpp/atomic/atomic