Namespaces
Variants

Concurrency support library

From cppreference.net
< c

C++ 包含对线程、原子操作、互斥锁、条件变量和线程局部存储的内置支持。

以下功能为可选提供:

  • 若编译器定义了宏常量 __STDC_NO_THREADS__ ,则头文件 <threads.h> 及其提供的所有名称均不可用;
  • 若编译器定义了宏常量 __STDC_NO_ATOMICS__ ,则头文件 <stdatomic.h> 及其提供的所有名称均不可用。

另请参阅 _Atomic 类型说明符与限定符

目录

线程

定义于头文件 <threads.h>
thrd_t 标识线程的实现定义完整对象类型
创建线程
(函数)
检查两个标识符是否引用同一线程
(函数)
获取当前线程标识符
(函数)
暂停调用线程执行指定时长
(函数)
让出当前时间片
(函数)
终止调用线程
(函数)
分离线程
(函数)
阻塞直至线程终止
(函数)
指示线程错误状态
(常量)
thrd_start_t
(C11)
函数指针类型 int ( * ) ( void * ) 的类型定义,由 thrd_create 使用
(类型定义)

原子操作

定义于头文件 <stdatomic.h>
原子类型操作
指示给定原子类型是否为无锁的
(宏常量)
指示原子对象是否为无锁的
(函数)
存储值到原子对象中
(函数)
从原子对象读取值
(函数)
交换原子对象的值
(函数)
若旧值符合预期则交换原子对象的值,否则读取旧值
(函数)
原子加法
(函数)
原子减法
(函数)
原子按位或
(函数)
原子按位异或
(函数)
原子按位与
(函数)
标志类型及操作
无锁原子布尔标志
(结构体)
设置 atomic_flag 为 true 并返回旧值
(函数)
设置 atomic_flag 为 false
(函数)
初始化
初始化现有原子对象
(函数)
(C11) (C17 中弃用) (C23 中移除)
初始化新原子对象
(函数宏)
初始化新的 atomic_flag
(宏常量)
便捷类型别名
类型定义名称 完整类型名称
atomic_bool (C11) _Atomic _Bool (C23前) _Atomic bool (C23起)
atomic_char (C11) _Atomic char
atomic_schar (C11) _Atomic signed char
atomic_uchar (C11) _Atomic unsigned char
atomic_short (C11) _Atomic short
atomic_ushort (C11) _Atomic unsigned short
atomic_int (C11) _Atomic int
atomic_uint (C11) _Atomic unsigned int
atomic_long (C11) _Atomic long
atomic_ulong (C11) _Atomic unsigned long
atomic_llong (C11) _Atomic long long
atomic_ullong (C11) _Atomic unsigned long long
atomic_char8_t (C23) _Atomic char8_t
atomic_char16_t (C11) _Atomic char16_t
atomic_char32_t (C11) _Atomic char32_t
atomic_wchar_t (C11) _Atomic wchar_t
atomic_int_least8_t (C11) _Atomic int_least8_t
atomic_uint_least8_t (C11) _Atomic uint_least8_t
atomic_int_least16_t (C11) _Atomic int_least16_t
atomic_uint_least16_t (C11) _Atomic uint_least16_t
atomic_int_least32_t (C11) _Atomic int_least32_t
atomic_uint_least32_t (C11) _Atomic uint_least32_t
atomic_int_least64_t (C11) _Atomic int_least64_t
atomic_uint_least64_t (C11) _Atomic uint_least64_t
atomic_int_fast8_t (C11) _Atomic int_fast8_t
atomic_uint_fast8_t (C11) _Atomic uint_fast8_t
atomic_int_fast16_t (C11) _Atomic int_fast16_t
atomic_uint_fast16_t (C11) _Atomic uint_fast16_t
atomic_int_fast32_t (C11) _Atomic int_fast32_t
atomic_uint_fast32_t (C11) _Atomic uint_fast32_t
atomic_int_fast64_t (C11) _Atomic int_fast64_t
atomic_uint_fast64_t (C11) _Atomic uint_fast64_t
atomic_intptr_t (C11) _Atomic intptr_t
atomic_uintptr_t (C11) _Atomic uintptr_t
atomic_size_t (C11) _Atomic size_t
atomic_ptrdiff_t (C11) _Atomic ptrdiff_t
atomic_intmax_t (C11) _Atomic intmax_t
atomic_uintmax_t (C11) _Atomic uintmax_t

互斥锁

定义于头文件 <threads.h>
mtx_t 互斥锁标识符
创建互斥锁
(函数)
阻塞直至锁定互斥锁
(函数)
阻塞直至锁定互斥锁或超时
(函数)
锁定互斥锁,若已被锁定则立即返回而不阻塞
(函数)
解锁互斥锁
(函数)
销毁互斥锁
(函数)
定义互斥锁类型
(枚举)
单次调用
精确调用函数一次
(函数)

条件变量

定义于头文件 <threads.h>
cnd_t 条件变量标识符
创建条件变量
(函数)
解除一个在条件变量上阻塞的线程
(函数)
解除所有在条件变量上阻塞的线程
(函数)
在条件变量上阻塞
(函数)
在条件变量上阻塞,带超时
(函数)
销毁条件变量
(函数)

线程局部存储

定义于头文件 <threads.h>
(C11) (C23中移除)
存储类说明符 _Thread_local 的便捷宏
(关键字宏)
tss_t 线程特定存储指针
析构函数被调用的最大次数
(宏常量)
tss_dtor_t
(C11)
函数指针类型 void ( * ) ( void * ) ,用于TSS析构函数
(类型定义)
创建带有给定析构函数的线程特定存储指针
(函数)
(C11)
从线程特定存储中读取
(函数)
(C11)
写入线程特定存储
(函数)
释放给定线程特定指针所持有的资源
(函数)

保留标识符

在C标准的未来修订中:

  • cnd_ mtx_ thrd_ tss_ 开头且后跟小写字母的函数名、类型名和枚举常量,可能会被添加到 <threads.h> 头文件的声明中;
  • ATOMIC_ 开头且后跟大写字母的宏,可能会被添加到 <stdatomic.h> 头文件定义的宏中;
  • atomic_ memory_ 开头且后跟小写字母的 typedef 名称,可能会被添加到 <stdatomic.h> 头文件的声明中;
  • memory_order_ 开头且后跟小写字母的枚举常量,可能会被添加到 <stdatomic.h> 头文件中 memory_order 类型的定义中;
  • atomic_ 开头且后跟小写字母的函数名,可能会被添加到 <stdatomic.h> 头文件的声明中。

保留用于函数名的标识符始终 可能 (C23起) 被保留作为具有外部链接的标识符使用,而此处列出的其他标识符在包含 <stdatomic.h> 可能 (C23起) 被保留使用。

声明、定义或 #undef 此类标识符将导致未定义行为 (若该标识符由标准或实现提供 (C23起) 。可移植程序不应使用这些标识符。

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.17 原子操作 <stdatomic.h> (页码: 待定)
  • 7.26 线程 <threads.h> (页码: 待定)
  • 7.31.8 原子操作 <stdatomic.h> (页码: 待定)
  • 7.31.15 线程 <threads.h> (页码: 待定)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.17 原子操作 <stdatomic.h> (页码: 200-209)
  • 7.26 线程 <threads.h> (页码: 274-283)
  • 7.31.8 原子操作 <stdatomic.h> (页码: 332)
  • 7.31.15 线程 <threads.h> (页码: 333)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.17 原子操作 <stdatomic.h> (页码: 273-286)
  • 7.26 线程 <threads.h> (页码: 376-387)
  • 7.31.8 原子操作 <stdatomic.h> (页码: 455-456)
  • 7.31.15 线程 <threads.h> (页码: 456)

参见

C++ 文档 关于 并发支持库

外部链接

GNU GCC Libc 手册:ISO C 互斥锁