다음을 통해 공유


<atomic>

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

#include <atomic>

설명

참고

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.

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

The template class atomic<Ty> stores an object of its argument type Ty 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<Ty *> 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<Ty> and can be passed to any of the non-member atomic functions.

atomic_integral 형식

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

설명

atomic 구조체

Describes an object that performs atomic operations on a stored value.

atomic_flag 구조체

설명 원자 단위로 설정하고 bool 플레그를 해제합니다.

열거형

Name

설명

memory_order Enum

메모리 위치에 동기화 연산에 대한 기호 이름을 제공합니다. 이러한 연산은 하나의 스레드의 할당이 다른 스레드에 표시 될 방법에 영향을 미칩니다.

함수

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

설명

atomic_compare_exchange_strong 함수

수행 된 원자 비교 및 교환 에서 작업합니다.

atomic_compare_exchange_strong_explicit 함수

수행 된 원자 비교 및 교환 에서 작업합니다.

atomic_compare_exchange_weak 함수

이 약한 원자 비교 및 교환 작업을 수행합니다.

atomic_compare_exchange_weak_explicit 함수

이 약한 원자 비교 및 교환 작업을 수행합니다.

atomic_exchange 함수

Replaces a stored value.

atomic_exchange_explicit 함수

Replaces a stored value.

atomic_fetch_add 함수

Adds a specified value to an existing stored value.

atomic_fetch_add_explicit 함수

Adds a specified value to an existing stored value.

atomic_fetch_and 함수

Performs a bitwise and on a specified value and an existing stored value.

atomic_fetch_and_explicit 함수

Performs a bitwise and on a specified value and an existing stored value.

atomic_fetch_or 함수

Performs a bitwise or on a specified value and an existing stored value.

atomic_fetch_or_explicit 함수

Performs a bitwise or on a specified value and an existing stored value.

atomic_fetch_sub 함수

Subtracts a specified value from an existing stored value.

atomic_fetch_sub_explicit 함수

Subtracts a specified value from an existing stored value.

atomic_fetch_xor 함수

Performs a bitwise exclusive or on a specified value and an existing stored value.

atomic_fetch_xor_explicit 함수

Performs a bitwise exclusive or on a specified value and an existing stored value.

atomic_flag_clear 함수

Sets the flag in an atomic_flag object to false.

atomic_flag_clear_explicit 함수

Sets the flag in an atomic_flag object to false.

atomic_flag_test_and_set 함수

Sets the flag in an atomic_flag object to true.

atomic_flag_test_and_set_explicit 함수

Sets the flag in an atomic_flag object to true.

atomic_init 함수

Sets the stored value in an atomic object.

atomic_is_lock_free 함수

Specifies whether atomic operations on a specified object are lock-free.

atomic_load 함수

Atomically retrieves a value.

atomic_load_explicit 함수

Atomically retrieves a value.

atomic_signal_fence 함수

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 함수

Atomically stores a value.

atomic_store_explicit 함수

Atomically stores a value.

atomic_thread_fence 함수

Acts as a fence that establishes memory ordering requirements with respect to other fences.

kill_dependency 함수

Breaks a possible dependency chain.

참고 항목

참조

표준 템플릿 라이브러리

기타 리소스

C++ 표준 라이브러리 헤더 파일