< cpp‎ | atomic
Revision as of 18:03, 2 November 2012 by P12bot (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <atomic>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class T >
struct atomic;

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
struct atomic<Integral>;

<td > (2) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
struct atomic<T*>;

<td > (3) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Each full specialization of the std::atomic template defines an atomic type. Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.

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

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

1) One 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 name Full specialization
std::atomic_bool std::atomic<bool>

2) Specializations and typedefs for integral types, as follows:

Typedef name Full specialization
std::atomic_char std::atomic<char>
std::atomic_schar std::atomic<signed char>
std::atomic_uchar std::atomic<unsigned char>
std::atomic_short std::atomic<short>
std::atomic_ushort std::atomic<unsigned short>
std::atomic_int std::atomic<int>
std::atomic_uint std::atomic<unsigned int>
std::atomic_long std::atomic<long>
std::atomic_ulong std::atomic<unsigned long>
std::atomic_llong std::atomic<long long>
std::atomic_ullong std::atomic<unsigned long long>
std::atomic_char16_t std::atomic<char16_t>
std::atomic_char32_t std::atomic<char32_t>
std::atomic_wchar_t std::atomic<wchar_t>
std::atomic_int_least8_t std::atomic<int_least8_t>
std::atomic_uint_least8_t std::atomic<uint_least8_t>
std::atomic_int_least16_t std::atomic<int_least16_t>
std::atomic_uint_least16_t std::atomic<uint_least16_t>
std::atomic_int_least32_t std::atomic<int_least32_t>
std::atomic_uint_least32_t std::atomic<uint_least32_t>
std::atomic_int_least64_t std::atomic<int_least64_t>
std::atomic_uint_least64_t std::atomic<uint_least64_t>
std::atomic_int_fast8_t std::atomic<int_fast8_t>
std::atomic_uint_fast8_t std::atomic<uint_fast8_t>
std::atomic_int_fast16_t std::atomic<int_fast16_t>
std::atomic_uint_fast16_t std::atomic<uint_fast16_t>
std::atomic_int_fast32_t std::atomic<int_fast32_t>
std::atomic_uint_fast32_t std::atomic<uint_fast32_t>
std::atomic_int_fast64_t std::atomic<int_fast64_t>
std::atomic_uint_fast64_t std::atomic<uint_fast64_t>
std::atomic_intptr_t std::atomic<intptr_t>
std::atomic_uintptr_t std::atomic<uintptr_t>
std::atomic_size_t std::atomic<size_t>
std::atomic_ptrdiff_t std::atomic<ptrdiff_t>
std::atomic_intmax_t std::atomic<intmax_t>
std::atomic_uintmax_t std::atomic<uintmax_t>

These specializations have standard layout, trivial default constructors, and trivial destructors. They support aggregate initialization syntax.

3) std::atomic<T*> for all pointer types. These specializations have standard layout, trivial default constructors, and trivial destructors. They support aggregate initialization syntax.


Member functions

Template:cpp/atomic/atomic/dcl list constructorTemplate:cpp/atomic/atomic/dcl list operator=Template:cpp/atomic/atomic/dcl list is lock freeTemplate:cpp/atomic/atomic/dcl list storeTemplate:cpp/atomic/atomic/dcl list loadTemplate:cpp/atomic/atomic/dcl list operator TTemplate:cpp/atomic/atomic/dcl list exchangeTemplate:cpp/atomic/atomic/dcl list compare exchange

Specialized member functions

Template:cpp/atomic/atomic/dcl list fetch addTemplate:cpp/atomic/atomic/dcl list fetch subTemplate:cpp/atomic/atomic/dcl list fetch andTemplate:cpp/atomic/atomic/dcl list fetch orTemplate:cpp/atomic/atomic/dcl list fetch xorTemplate:cpp/atomic/atomic/dcl list operator arithTemplate:cpp/atomic/atomic/dcl list operator arith2


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>.

See also

Template:cpp/atomic/dcl list atomic flag