次の方法で共有


<atomic>

 

The latest version of this topic can be found at <atomic>.

Defines classes and template classes to use to create types that support atomic operations.

Syntax

#include <atomic>  

Remarks

Note

In code that's compiled by using /clr or /clr:pure, this header is blocked.

An atomic operation has two key properties that help you use multiple threads to correctly manipulate an object without using mutex locks.

  • Because an atomic operation is indivisible, a second atomic operation on the same object from a different thread can obtain the state of the object only before or after the first atomic operation.

  • Based on its memory_order argument, an atomic operation establishes ordering requirements for the visibility of the effects of other atomic operations in the same thread. Consequently, it inhibits compiler optimizations that violate the ordering requirements.

On some platforms, it might not be possible to efficiently implement atomic operations for some types without using mutex locks. An atomic type is lock-free if no atomic operations on that type use locks.

C++11: In signal-handlers you can perform atomic operations on an object obj if obj.is_lock_free() or atomic_is_lock_free(x) are true.

The class atomic_flag provides a minimal atomic type that holds a bool flag. Its operations are always lock-free.

The template class atomic<T> stores an object of its argument type T and provides atomic access to that stored value. You can instantiate it by using any type that can be copied by using memcpy and tested for equality by using memcmp. In particular, you can use it with user-defined types that meet these requirements and, in many cases, with floating-point types.

The template also has a set of specializations for integral types and a partial specialization for pointers. These specializations provide additional operations that are not available through the primary template.

Pointer Specializations

The atomic<T *> partial specializations apply to all pointer types. They provide methods for pointer arithmetic.

Integral Specializations

The atomic<integral> specializations apply to all integral types. They provide additional operations that are not available through the primary template.

Each atomic<integral> type has a corresponding macro that you can use in an if directive to determine at compile time whether operations on that type are lock-free. If the value of the macro is zero, operations on the type are not lock-free. If the value is 1, operations might be lock-free, and a runtime check is required. If the value is 2, operations are lock-free. You can use the function atomic_is_lock_free to determine at runtime whether operations on the type are lock-free.

For each of the integral types, there is a corresponding named atomic type that manages an object of that integral type. Each atomic_integral type has the same set of member functions as the corresponding instantiation of atomic<T> and can be passed to any of the non-member atomic functions.

atomic_integral Type Integral Type atomic_is_lock_free Macro
atomic_char char ATOMIC_CHAR_LOCK_FREE
atomic_schar signed char ATOMIC_CHAR_LOCK_FREE
atomic_uchar unsigned char ATOMIC_CHAR_LOCK_FREE
atomic_char16_t char16_t ATOMIC_CHAR16_T_LOCK_FREE
atomic_char32_t char32_t ATOMIC_CHAR32_T_LOCK_FREE
atomic_wchar_t wchar_t ATOMIC_WCHAR_T_LOCK_FREE
atomic_short short ATOMIC_SHORT_LOCK_FREE
atomic_ushort unsigned short ATOMIC_SHORT_LOCK_FREE
atomic_int int ATOMIC_INT_LOCK_FREE
atomic_uint unsigned int ATOMIC_INT_LOCK_FREE
atomic_long long ATOMIC_LONG_LOCK_FREE
atomic_ulong unsigned long ATOMIC_LONG_LOCK_FREE
atomic_llong long long ATOMIC_LLONG_LOCK_FREE
atomic_ullong unsigned long long ATOMIC_LLONG_LOCK_FREE

Typedef names exist for specializations of the atomic template for some of the types that are defined in the header <inttypes.h>.

Atomic Type Typedef Name
atomic_int8_t atomic<int8_t>
atomic_uint8_t atomic<uint8_t>
atomic_int16_t atomic<int16_t>
atomic_uint16_t atomic<uint16_t>
atomic_int32_t atomic<int32_t>
atomic_uint32_t atomic<uint32_t>
atomic_int64_t atomic<int64_t>
atomic_uint64_t atomic<uint64_t>
atomic_int_least8_t atomic<int_least8_t>
atomic_uint_least8_t atomic<uint_least8_t>
atomic_int_least16_t atomic<int_least16_t>
atomic_uint_least16_t atomic<uint_least16_t>
atomic_int_least32_t atomic<int_least32_t>
atomic_uint_least32_t atomic<uint_least32_t>
atomic_int_least64_t atomic<int_least64_t>
atomic_uint_least64_t atomic<uint_least64_t>
atomic_int_fast8_t atomic<int_fast8_t>
atomic_uint_fast8_t atomic<uint_fast8_t>
atomic_int_fast16_t atomic<int_fast16_t>
atomic_uint_fast16_ atomic<uint_fast16_t>
atomic_int_fast32_t atomic<int_fast32_t>
atomic_uint_fast32_t atomic<uint_fast32_t>
atomic_int_fast64_t atomic<int_fast64_t>
atomic_uint_fast64_t atomic<uint_fast64_t>
atomic_intptr_t atomic<intptr_t>
atomic_uintptr_t atomic<uintptr_t>
atomic_size_t atomic<size_t>
atomic_ptrdiff_t atomic<ptrdiff_t>
atomic_intmax_t atomic<intmax_t>
atomic_uintmax_t atomic<uintmax_t>

Structs

Name Description
atomic Structure Describes an object that performs atomic operations on a stored value.
atomic_flag Structure Describes an object that atomically sets and clears a bool flag.

Enums

Name Description
memory_order Enum Supplies symbolic names for synchronization operations on memory locations. These operations affect how assignments in one thread become visible in another.

Functions

In the following list, the functions that do not end in _explicit have the semantics of the corresponding _explicit, except that they have the implicit memory_order arguments of memory_order_seq_cst.

Name Description
atomic_compare_exchange_strong Function Performs an atomic compare and exchange operation.
atomic_compare_exchange_strong_explicit Function Performs an atomic compare and exchange operation.
atomic_compare_exchange_weak Function Performs a weak atomic compare and exchange operation.
atomic_compare_exchange_weak_explicit Function Performs a weak atomic compare and exchange operation.
atomic_exchange Function Replaces a stored value.
atomic_exchange_explicit Function Replaces a stored value.
atomic_fetch_add Function Adds a specified value to an existing stored value.
atomic_fetch_add_explicit Function Adds a specified value to an existing stored value.
atomic_fetch_and Function Performs a bitwise and on a specified value and an existing stored value.
atomic_fetch_and_explicit Function Performs a bitwise and on a specified value and an existing stored value.
atomic_fetch_or Function Performs a bitwise or on a specified value and an existing stored value.
atomic_fetch_or_explicit Function Performs a bitwise or on a specified value and an existing stored value.
atomic_fetch_sub Function Subtracts a specified value from an existing stored value.
atomic_fetch_sub_explicit Function Subtracts a specified value from an existing stored value.
atomic_fetch_xor Function Performs a bitwise exclusive or on a specified value and an existing stored value.
atomic_fetch_xor_explicit Function Performs a bitwise exclusive or on a specified value and an existing stored value.
atomic_flag_clear Function Sets the flag in an atomic_flag object to false.
atomic_flag_clear_explicit Function Sets the flag in an atomic_flag object to false.
atomic_flag_test_and_set Function Sets the flag in an atomic_flag object to true.
atomic_flag_test_and_set_explicit Function Sets the flag in an atomic_flag object to true.
atomic_init Function Sets the stored value in an atomic object.
atomic_is_lock_free Function Specifies whether atomic operations on a specified object are lock-free.
atomic_load Function Atomically retrieves a value.
atomic_load_explicit Function Atomically retrieves a value.
atomic_signal_fence Function Acts as a fence that establishes memory ordering requirements between fences in a calling thread that has signal handlers executed in the same thread.
atomic_store Function Atomically stores a value.
atomic_store_explicit Function Atomically stores a value.
atomic_thread_fence Function Acts as a fence that establishes memory ordering requirements with respect to other fences.
kill_dependency Function Breaks a possible dependency chain.

See Also

Header Files Reference
Standard Template Library