命名空间
变体
操作

std::atomic

来自 cppreference.cn
< cpp‎ | atomic
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协同取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
门闩和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危险指针
原子类型
atomic
(C++11)
(C++20)
原子类型的初始化
(C++11)(C++20 中已弃用)
(C++11)(C++20 中已弃用)
内存排序
(C++11)(C++26 中已弃用)
原子操作的自由函数
原子标志的自由函数
 
 
定义于头文件 <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> 中提供了兼容宏 _Atomic,使得 _Atomic(T)std::atomic<T> 相同,同时两者都是良好形成的。

当包含 <stdatomic.h> 时,命名空间 std 中的任何声明是否可用是未指定的。

(C++23 起)

目录

[编辑] 特化

[编辑] 主模板

std::atomic 模板可以使用任何满足 CopyConstructibleCopyAssignable可简单复制 (TriviallyCopyable) 类型 T 进行实例化。如果以下任何值为 false,则程序是非良构的

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 模板提供了部分特化,这些类型具有主模板不具备的额外属性

2) 针对所有指针类型的部分特化 std::atomic<U*>。这些特化具有标准布局、平凡默认构造函数,(C++20 前)和平凡析构函数。除了为所有原子类型提供的操作之外,这些特化还额外支持适用于指针类型的原子算术操作,例如 fetch_addfetch_sub
3,4)std::shared_ptrstd::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_addfetch_subfetch_andfetch_orfetch_xor

  • 字符类型 charchar8_t(C++20 起)char16_tchar32_twchar_t
  • 标准有符号整数类型:signed charshortintlonglong long
  • 标准无符号整数类型:unsigned charunsigned shortunsigned intunsigned longunsigned long long
  • 头文件 <cstdint> 中的 typedefs 所需的任何其他整数类型。

此外,由此产生的 std::atomic<Integral> 特化具有标准布局、平凡默认构造函数,(C++20 前)和平凡析构函数。有符号整数算术定义为使用二进制补码;没有未定义的结果。

浮点类型的特化

当实例化为无 cv 限定的浮点类型(floatdoublelong double 以及无 cv 限定的扩展浮点类型(C++23 起))时,std::atomic 提供了适用于浮点类型的额外原子操作,例如 fetch_addfetch_sub

此外,由此产生的 std::atomic<Floating> 特化具有标准布局和平凡析构函数。

即使结果无法在浮点类型中表示,也没有操作会导致未定义行为。浮点环境可能与调用线程的浮点环境不同。

(C++20 起)

[编辑] 成员类型

类型 定义
value_type T (无论是否特化)
difference_type[1]

value_type (仅适用于 atomic<Integral>atomic<Floating>(C++20 起) 特化)

std::ptrdiff_t (仅适用于 std::atomic<U*> 特化)

  1. difference_type 未在主 std::atomic 模板或 std::shared_ptrstd::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) [编辑]
通知至少一个等待原子对象的线程
(public member function) [编辑]
通知所有被原子对象阻塞的线程
(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 和所有上述整数类型提供了类型别名,如下所示

所有 std::atomic<Integral> 的别名
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_tstd::uintN_tstd::intptr_tstd::uintptr_t 时,才定义 std::atomic_intN_tstd::atomic_uintN_tstd::atomic_intptr_tstd::atomic_uintptr_t

std::atomic_signed_lock_freestd::atomic_unsigned_lock_free 在独立实现中是可选的。

(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::atomicstd::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 文档 关于 原子类型