12/06/2019
本文内容
定义用于创建支持原子操作的类型的类和类模板。Defines classes and class templates to use to create types that support atomic operations.
语法Syntax
#include
备注Remarks
备注
在使用/clr: pure编译的代码中,此标头被阻止。In code that's compiled by using /clr:pure, this header is blocked. 在 Visual Studio 2017 及更高版本中,不推荐使用 /clr: pure和 /clr: safe 。Both /clr:pure and /clr:safe are deprecated in Visual Studio 2017 and later versions.
一个原子操作有两个关键属性,帮助你使用多个线程正确操控对象,而无需使用互斥锁。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 object's state only before or after the first atomic operation.
基于其 memory_order 参数,原子操作可以针对同一个线程中其他原子操作的影响可见性建立排序要求。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.
在某些平台上,如果不使用 mutex 锁,可能无法有效地实施某些类型的原子操作。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:在信号处理程序中,如果或为 true,则可以对对象执行原子操作 obj obj.is_lock_free() atomic_is_lock_free(x) 。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.
类atomic_flag提供保存标志的最小原子类型 bool 。The class atomic_flag provides a minimal atomic type that holds a bool flag. 其操作始终为无锁操作。Its operations are always lock-free.
类模板 atomic 存储其参数类型的对象 T ,并提供对该存储值的原子访问。The class template atomic stores an object of its argument type T and provides atomic access to that stored value. 你可以使用可通过 memcpy 复制的任何类型对该类进行实例化,并通过使用 memcmp 测试是否相等。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 aren't available through the primary template.
指针专用化Pointer Specializations
atomic 部分专用化应用于所有指针类型。The atomic partial specializations apply to all pointer types. 它们提供用于指针算术的方法。They provide methods for pointer arithmetic.
整型专用化Integral Specializations
atomic 专用化将应用于所有整型类型。The atomic specializations apply to all integral types. 它们提供了无法通过主模板获得的其他操作。They provide additional operations that aren't available through the primary template.
每个 atomic 类型都有一个对应的宏,你可以在 if directive 中使用该宏来确定编译时对该类型执行的操作是否为无锁。Each atomic 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 aren't lock-free. 如果值为 1,则操作可能为无锁,且需要进行运行时检查。If the value is 1, operations might be lock-free, and a runtime check is required. 如果值为 2,操作为无锁。If the value is 2, operations are lock-free. 可以使用函数 atomic_is_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's a corresponding named atomic type that manages an object of that integral type. 每个 atomic_integral 类型都具有与 atomic 的相应实例化相同的成员函数集,并且可以传递至任何非成员原子函数。Each atomic_integral type has the same set of member functions as the corresponding instantiation of atomic and can be passed to any of the non-member atomic functions.
atomic_integral 类型atomic_integral Type
整型类型Integral Type
atomic_is_lock_free 宏atomic_is_lock_free Macro
atomic_char
char
ATOMIC_CHAR_LOCK_FREEATOMIC_CHAR_LOCK_FREE
atomic_schar
signed char
ATOMIC_CHAR_LOCK_FREEATOMIC_CHAR_LOCK_FREE
atomic_uchar
unsigned char
ATOMIC_CHAR_LOCK_FREEATOMIC_CHAR_LOCK_FREE
atomic_char16_t
char16_t
ATOMIC_CHAR16_T_LOCK_FREEATOMIC_CHAR16_T_LOCK_FREE
atomic_char32_t
char32_t
ATOMIC_CHAR32_T_LOCK_FREEATOMIC_CHAR32_T_LOCK_FREE
atomic_wchar_t
wchar_t
ATOMIC_WCHAR_T_LOCK_FREEATOMIC_WCHAR_T_LOCK_FREE
atomic_short
short
ATOMIC_SHORT_LOCK_FREEATOMIC_SHORT_LOCK_FREE
atomic_ushort
unsigned short
ATOMIC_SHORT_LOCK_FREEATOMIC_SHORT_LOCK_FREE
atomic_int
int
ATOMIC_INT_LOCK_FREEATOMIC_INT_LOCK_FREE
atomic_uint
unsigned int
ATOMIC_INT_LOCK_FREEATOMIC_INT_LOCK_FREE
atomic_long
long
ATOMIC_LONG_LOCK_FREEATOMIC_LONG_LOCK_FREE
atomic_ulong
unsigned long
ATOMIC_LONG_LOCK_FREEATOMIC_LONG_LOCK_FREE
atomic_llong
long long
ATOMIC_LLONG_LOCK_FREEATOMIC_LLONG_LOCK_FREE
atomic_ullong
unsigned long long
ATOMIC_LLONG_LOCK_FREEATOMIC_LLONG_LOCK_FREE
在标头中定义的某些类型的原子模板专用化 Typedef 名称 。Typedef names exist for specializations of the atomic template for some of the types that are defined in the header .
原子类型Atomic Type
Typedef 名称Typedef Name
atomic_int8_t
atomic
atomic_uint8_t
atomic
atomic_int16_t
atomic
atomic_uint16_t
atomic
atomic_int32_t
atomic
atomic_uint32_t
atomic
atomic_int64_t
atomic
atomic_uint64_t
atomic
atomic_int_least8_t
atomic
atomic_uint_least8_t
atomic
atomic_int_least16_t
atomic
atomic_uint_least16_t
atomic
atomic_int_least32_t
atomic
atomic_uint_least32_t
atomic
atomic_int_least64_t
atomic
atomic_uint_least64_t
atomic
atomic_int_fast8_t
atomic
atomic_uint_fast8_t
atomic
atomic_int_fast16_t
atomic
atomic_uint_fast16_
atomic
atomic_int_fast32_t
atomic
atomic_uint_fast32_t
atomic
atomic_int_fast64_t
atomic
atomic_uint_fast64_t
atomic
atomic_intptr_t
atomic
atomic_uintptr_t
atomic
atomic_size_t
atomic
atomic_ptrdiff_t
atomic
atomic_intmax_t
atomic
atomic_uintmax_t
atomic
结构Structs
名称Name
描述Description
描述对存储值执行原子操作的对象。Describes an object that performs atomic operations on a stored value.
描述一个对象,该对象以原子方式设置并清除 bool 标志。Describes an object that atomically sets and clears a bool flag.
枚举Enums
名称Name
描述Description
为内存位置上的同步操作提供符号名称。Supplies symbolic names for synchronization operations on memory locations. 这些操作将影响一个线程内的分配如何在另一个线程内变得可见。These operations affect how assignments in one thread become visible in another.
函数Functions
在下面的列表中,不以结尾的函数 _explicit 具有对应的语义 _explicit ,只不过它们具有的隐式memory_order自变量 memory_order_seq_cst 。In the following list, the functions that don't 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
执行原子比较和交换操作。Performs an atomic compare and exchange operation.
执行弱原子比较和交换操作。Performs a weak atomic compare and exchange operation.
替换存储值。Replaces a stored value.
将指定的值添加到现有存储值。Adds a specified value to an existing stored value.
将指定的值添加到现有存储值。Adds a specified value to an existing stored value.
对指定值和现有存储值执行按位 and。Performs a bitwise and on a specified value and an existing stored value.
对指定值和现有存储值执行按位 and。Performs a bitwise and on a specified value and an existing stored value.
对指定值和现有存储值执行按位 or。Performs a bitwise or on a specified value and an existing stored value.
对指定值和现有存储值执行按位 or。Performs a bitwise or on a specified value and an existing stored value.
从现有存储值减去指定的值。Subtracts a specified value from an existing stored value.
从现有存储值减去指定的值。Subtracts a specified value from an existing stored value.
对指定值和现有存储值执行按位 exclusive or。Performs a bitwise exclusive or on a specified value and an existing stored value.
对指定值和现有存储值执行按位 exclusive or。Performs a bitwise exclusive or on a specified value and an existing stored value.
将对象中的标志设置 atomic_flag 为 false 。Sets the flag in an atomic_flag object to false.
将对象中的标志设置 atomic_flag 为 false 。Sets the flag in an atomic_flag object to false.
将对象中的标志设置 atomic_flag 为 true 。Sets the flag in an atomic_flag object to true.
将对象中的标志设置 atomic_flag 为 true 。Sets the flag in an atomic_flag object to true.
设置 atomic 对象中存储的值。Sets the stored value in an atomic object.
指定对指定对象执行的原子操作是否为无锁。Specifies whether atomic operations on a specified object are lock-free.
以原子方式检索一个值。Atomically retrieves a value.
以原子方式检索一个值。Atomically retrieves a value.
充当 fence,用于在调用线程中信号处理程序在同一线程中执行的 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.
以原子方式存储一个值。Atomically stores a value.
充当就其他 fence 建立内存排序要求的 fence。Acts as a fence that establishes memory ordering requirements with respect to other fences.
中断可能的依赖关系链。Breaks a possible dependency chain.
另请参阅See also