std::atomic
定义于头文件 <atomic> |
||
template< class T > struct atomic; |
(1) | (C++11 起) |
template< class U > struct atomic<U*>; |
(2) | (C++11 起) |
定义于头文件 <memory> |
||
template< class U > struct atomic<std::shared_ptr<U>>; |
(3) | (C++20 起) |
template< class U > struct atomic<std::weak_ptr<U>>; |
(4) | (C++20 起) |
Defined in header <stdatomic.h> |
||
#define _Atomic(T) /* 见下文 */ |
(5) | (C++23 起) |
std::atomic
模板的每个实例化和完全特化都定义了一个原子类型。如果一个线程写入一个原子对象,而另一个线程从它读取,则行为是定义良好的(有关数据竞争的详细信息,请参见内存模型)。
此外,对原子对象的访问可以建立线程间同步并按 std::memory_order 指定的顺序访问非原子内存。
std::atomic
既不可复制也不可移动。
在 <stdatomic.h> 中提供了兼容宏 当包含 <stdatomic.h> 时,命名空间 |
(C++23 起) |
目录 |
[编辑] 特化
[编辑] 主模板
主 std::atomic
模板可以使用任何满足 CopyConstructible 和 CopyAssignable 的 可简单复制 (TriviallyCopyable) 类型 T
进行实例化。如果以下任何值为 false,则程序是非良构的
- std::is_trivially_copyable<T>::value
- std::is_copy_constructible<T>::value
- std::is_move_constructible<T>::value
- std::is_copy_assignable<T>::value
- std::is_move_assignable<T>::value
- std::is_same<T, typename std::remove_cv<T>::type>::value
struct Counters { int a; int b; }; // user-defined trivially-copyable type std::atomic<Counters> cnt; // specialization for the user-defined type
std::atomic<bool> 使用主模板。它保证是一个标准布局结构并具有平凡析构函数。
[编辑] 部分特化
标准库为以下类型的 std::atomic
模板提供了部分特化,这些类型具有主模板不具备的额外属性
std::atomic<U*>
。这些特化具有标准布局、平凡默认构造函数,(C++20 前)和平凡析构函数。除了为所有原子类型提供的操作之外,这些特化还额外支持适用于指针类型的原子算术操作,例如 fetch_add
、fetch_sub
。
3,4) 为 std::shared_ptr 和 std::weak_ptr 提供了部分特化 std::atomic<std::shared_ptr<U>> 和 std::atomic<std::weak_ptr<U>>。
详见 std::atomic<std::shared_ptr> 和 std::atomic<std::weak_ptr>。 |
(C++20 起) |
[编辑] 整数类型的特化
当实例化为以下整数类型之一时,std::atomic
提供了适用于整数类型的额外原子操作,例如 fetch_add
、fetch_sub
、fetch_and
、fetch_or
、fetch_xor
- 字符类型 char、char8_t(C++20 起)、char16_t、char32_t 和 wchar_t;
- 标准有符号整数类型:signed char、short、int、long 和 long long;
- 标准无符号整数类型:unsigned char、unsigned short、unsigned int、unsigned long 和 unsigned long long;
- 头文件 <cstdint> 中的 typedefs 所需的任何其他整数类型。
此外,由此产生的 std::atomic<Integral>
特化具有标准布局、平凡默认构造函数,(C++20 前)和平凡析构函数。有符号整数算术定义为使用二进制补码;没有未定义的结果。
浮点类型的特化当实例化为无 cv 限定的浮点类型(float、double、long double 以及无 cv 限定的扩展浮点类型(C++23 起))时, 此外,由此产生的 即使结果无法在浮点类型中表示,也没有操作会导致未定义行为。浮点环境可能与调用线程的浮点环境不同。 |
(C++20 起) |
[编辑] 成员类型
类型 | 定义 | ||||
value_type
|
T (无论是否特化) | ||||
difference_type [1] |
|
- ↑
difference_type
未在主std::atomic
模板或 std::shared_ptr 和 std::weak_ptr 的部分特化中定义。
[编辑] 成员函数
constructs an atomic object (public member function) | |
stores a value into an atomic object (public member function) | |
检查原子对象是否为无锁 (public member function) | |
原子地将原子对象的值替换为非原子参数 (public member function) | |
atomically obtains the value of the atomic object (public member function) | |
loads a value from an atomic object (public member function) | |
atomically replaces the value of the atomic object and obtains the value held previously (public member function) | |
原子地将原子对象的值与非原子参数进行比较,如果相等则执行原子交换,否则执行原子加载 (public member function) | |
(C++20) |
阻塞线程直到被通知且原子值改变 (public member function) |
(C++20) |
通知至少一个等待原子对象的线程 (public member function) |
(C++20) |
通知所有被原子对象阻塞的线程 (public member function) |
常量 | |
[static] (C++17) |
指示该类型始终是无锁的 (public static member constant) |
[编辑] 专用成员函数
针对整数、浮点(C++20 起)和指针类型特化 | |
原子地将参数添加到原子对象中存储的值,并获取之前持有的值 (public member function) | |
原子地从原子对象中存储的值中减去参数,并获取之前持有的值 (public member function) | |
对原子值进行加减 (public member function) | |
仅针对整数和指针类型特化 | |
(C++26) |
原子地在参数和原子对象的值之间执行 std::max,并获取之前持有的值 (public member function) |
(C++26) |
原子地在参数和原子对象的值之间执行 std::min,并获取之前持有的值 (public member function) |
将原子值递增或递减一 (public member function) | |
Specialized for integral types only | |
原子地在参数和原子对象的值之间执行按位与运算,并获取之前持有的值 (public member function) | |
原子地在参数和原子对象的值之间执行按位或运算,并获取之前持有的值 (public member function) | |
原子地在参数和原子对象的值之间执行按位异或运算,并获取之前持有的值 (public member function) | |
对原子值执行按位与、或、异或运算 (public member function) |
[编辑] 类型别名
为 bool 和所有上述整数类型提供了类型别名,如下所示
所有
| |
atomic_bool (C++11) |
std::atomic<bool> (typedef) |
atomic_char (C++11) |
std::atomic<char> (typedef) |
atomic_schar (C++11) |
std::atomic<signed char> (typedef) |
atomic_uchar (C++11) |
std::atomic<unsigned char> (typedef) |
atomic_short (C++11) |
std::atomic<short> (typedef) |
atomic_ushort (C++11) |
std::atomic<unsigned short> (typedef) |
atomic_int (C++11) |
std::atomic<int> (typedef) |
atomic_uint (C++11) |
std::atomic<unsigned int> (typedef) |
atomic_long (C++11) |
std::atomic<long> (typedef) |
atomic_ulong (C++11) |
std::atomic<unsigned long> (typedef) |
atomic_llong (C++11) |
std::atomic<long long> (typedef) |
atomic_ullong (C++11) |
std::atomic<unsigned long long> (typedef) |
atomic_char8_t (C++20) |
std::atomic<char8_t> (typedef) |
atomic_char16_t (C++11) |
std::atomic<char16_t> (typedef) |
atomic_char32_t (C++11) |
std::atomic<char32_t> (typedef) |
atomic_wchar_t (C++11) |
std::atomic<wchar_t> (typedef) |
atomic_int8_t (C++11)(可选) |
std::atomic<std::int8_t> (typedef) |
atomic_uint8_t (C++11)(可选) |
std::atomic<std::uint8_t> (typedef) |
atomic_int16_t (C++11)(可选) |
std::atomic<std::int16_t> (typedef) |
atomic_uint16_t (C++11)(可选) |
std::atomic<std::uint16_t> (typedef) |
atomic_int32_t (C++11)(可选) |
std::atomic<std::int32_t> (typedef) |
atomic_uint32_t (C++11)(可选) |
std::atomic<std::uint32_t> (typedef) |
atomic_int64_t (C++11)(可选) |
std::atomic<std::int64_t> (typedef) |
atomic_uint64_t (C++11)(可选) |
std::atomic<std::uint64_t> (typedef) |
atomic_int_least8_t (C++11) |
std::atomic<std::int_least8_t> (typedef) |
atomic_uint_least8_t (C++11) |
std::atomic<std::uint_least8_t> (typedef) |
atomic_int_least16_t (C++11) |
std::atomic<std::int_least16_t> (typedef) |
atomic_uint_least16_t (C++11) |
std::atomic<std::uint_least16_t> (typedef) |
atomic_int_least32_t (C++11) |
std::atomic<std::int_least32_t> (typedef) |
atomic_uint_least32_t (C++11) |
std::atomic<std::uint_least32_t> (typedef) |
atomic_int_least64_t (C++11) |
std::atomic<std::int_least64_t> (typedef) |
atomic_uint_least64_t (C++11) |
std::atomic<std::uint_least64_t> (typedef) |
atomic_int_fast8_t (C++11) |
std::atomic<std::int_fast8_t> (typedef) |
atomic_uint_fast8_t (C++11) |
std::atomic<std::uint_fast8_t> (typedef) |
atomic_int_fast16_t (C++11) |
std::atomic<std::int_fast16_t> (typedef) |
atomic_uint_fast16_t (C++11) |
std::atomic<std::uint_fast16_t> (typedef) |
atomic_int_fast32_t (C++11) |
std::atomic<std::int_fast32_t> (typedef) |
atomic_uint_fast32_t (C++11) |
std::atomic<std::uint_fast32_t> (typedef) |
atomic_int_fast64_t (C++11) |
std::atomic<std::int_fast64_t> (typedef) |
atomic_uint_fast64_t (C++11) |
std::atomic<std::uint_fast64_t> (typedef) |
atomic_intptr_t (C++11)(可选) |
std::atomic<std::intptr_t> (typedef) |
atomic_uintptr_t (C++11)(可选) |
std::atomic<std::uintptr_t> (typedef) |
atomic_size_t (C++11) |
std::atomic<std::size_t> (typedef) |
atomic_ptrdiff_t (C++11) |
std::atomic<std::ptrdiff_t> (typedef) |
atomic_intmax_t (C++11) |
std::atomic<std::intmax_t> (typedef) |
atomic_uintmax_t (C++11) |
std::atomic<std::uintmax_t> (typedef) |
特殊用途类型的别名 | |
atomic_signed_lock_free (C++20) |
一个无锁且等待/通知效率最高的有符号整型原子类型 (typedef) |
atomic_unsigned_lock_free (C++20) |
一个无锁且等待/通知效率最高的无符号整型原子类型 (typedef) |
std::intN_t
、std::uintN_t
、std::intptr_t 和 std::uintptr_t 时,才定义 std::atomic_intN_t
、std::atomic_uintN_t
、std::atomic_intptr_t
和 std::atomic_uintptr_t
。
|
(C++20 起) |
[编辑] 注意
std::atomic
的所有成员函数都有非成员函数模板等效项。这些非成员函数可以额外重载用于非 std::atomic
特化但能保证原子性的类型。标准库中唯一的此类类型是 std::shared_ptr<U>。
_Atomic
是一个 关键字,用于在 C 中提供 原子类型。
建议实现确保 C 中 _Atomic(T)
的表示形式与 C++ 中 std::atomic<T>
的表示形式对于每种可能的类型 T
都是相同的。用于确保原子性和内存顺序的机制应兼容。
在 GCC 和 Clang 上,这里描述的一些功能需要链接 -latomic
。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_atomic_ref |
201806L |
(C++20) | std::atomic_ref
|
__cpp_lib_constexpr_atomic |
202411L |
(C++26) | constexpr std::atomic 和 std::atomic_ref |
[编辑] 示例
#include <atomic> #include <iostream> #include <thread> #include <vector> std::atomic_int acnt; int cnt; void f() { for (auto n{10000}; n; --n) { ++acnt; ++cnt; // Note: for this example, relaxed memory order is sufficient, // e.g. acnt.fetch_add(1, std::memory_order_relaxed); } } int main() { { std::vector<std::jthread> pool; for (int n = 0; n < 10; ++n) pool.emplace_back(f); } std::cout << "The atomic counter is " << acnt << '\n' << "The non-atomic counter is " << cnt << '\n'; }
可能的输出
The atomic counter is 100000 The non-atomic counter is 69696
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2441 | C++11 | 可选的原子版本的 typedef 固定宽度整型 缺失 |
已添加 |
LWG 3012 | C++11 | std::atomic<T> 允许用于任何可平凡复制但不可复制的 T |
此类特化被禁止 |
LWG 3949 | C++17 | 要求 std::atomic<bool> 具有 平凡析构函数的措辞在 C++17 中意外删除 |
已添加回来 |
LWG 4069 (P3323R1) |
C++11 | 对 cv 限定 T 的支持存在疑问 |
禁止 T 为 cv 限定 |
P0558R1 | C++11 | 一些函数模板参数推导 对于原子类型可能会意外 失败;提供了无效的指针操作 |
规范被大幅重写 添加了成员 typedef value_type 和 difference_type |
[编辑] 另请参阅
(C++11) |
无锁布尔原子类型 (类) |
(C++20) |
原子共享指针 (类模板特化) |
(C++20) |
原子弱指针 (类模板特化) |
C 文档 关于 原子类型
|