并发支持库
来自 cppreference.cn
< c
C 语言内建支持线程、原子操作、互斥、条件变量和线程特定存储。
这些特性是可选提供的
- 如果宏常量
__STDC_NO_THREADS__
由编译器定义,则头文件 <threads.h> 及其提供的所有名称将不会提供; - 如果宏常量
__STDC_NO_ATOMICS__
由编译器定义,则头文件 <stdatomic.h> 及其提供的所有名称将不会提供。
另请参见 _Atomic
类型说明符和限定符。
内容 |
[编辑] 线程
定义于头文件
<threads.h> | |
thrd_t
|
实现定义的完整对象类型,用于标识线程 |
(C11) |
创建线程 (函数) |
(C11) |
检查两个标识符是否引用同一个线程 (函数) |
(C11) |
获取当前线程标识符 (函数) |
(C11) |
暂停调用线程的执行指定时间 (函数) |
(C11) |
让出当前时间片 (函数) |
(C11) |
终止调用线程 (函数) |
(C11) |
分离线程 (函数) |
(C11) |
阻塞直到线程终止 (函数) |
指示线程错误状态 (常量) | |
thrd_start_t (C11) |
函数指针类型 int(*)(void*) 的 typedef,由 thrd_create 使用 (类型定义) |
[编辑] 原子操作
定义于头文件
<stdatomic.h> | |
原子类型上的操作 | |
指示给定的原子类型是否为无锁的 (宏常量) | |
(C11) |
指示原子对象是否为无锁的 (函数) |
将值存储到原子对象中 (函数) | |
从原子对象读取值 (函数) | |
将原子对象的值与新值交换 (函数) | |
如果旧值是期望值,则与原子对象交换值,否则读取旧值 (函数) | |
原子加法 (函数) | |
原子减法 (函数) | |
原子按位或 (函数) | |
原子按位异或 (函数) | |
原子按位与 (函数) | |
标志类型和操作 | |
(C11) |
无锁原子布尔标志 (结构体) |
将 atomic_flag 设置为 true 并返回旧值 (函数) | |
将 atomic_flag 设置为 false (函数) | |
初始化 | |
(C11) |
初始化已存在的原子对象 (函数) |
(C11)(在 C17 中弃用)(在 C23 中移除) |
初始化新的原子对象 (函数宏) |
(C11) |
初始化新的 atomic_flag (宏常量) |
内存同步顺序 | |
(C11) |
定义内存顺序约束 (枚举) |
(C11) |
打破 memory_order_consume 的依赖链 (函数宏) |
(C11) |
通用内存顺序依赖的栅栏同步原语 (函数) |
(C11) |
线程和在同一线程中执行的信号处理程序之间的栅栏 (函数) |
便捷类型别名 | |
类型别名 | 完整类型名称 |
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
|
互斥锁标识符 |
(C11) |
创建互斥锁 (函数) |
(C11) |
阻塞直到锁定互斥锁 (函数) |
(C11) |
阻塞直到锁定互斥锁或超时 (函数) |
(C11) |
锁定互斥锁或在已被锁定时立即返回 (函数) |
(C11) |
解锁互斥锁 (函数) |
(C11) |
销毁互斥锁 (函数) |
(C11)(C11)(C11) |
定义互斥锁的类型 (枚举) |
单次调用 | |
(C11) |
精确调用函数一次 (函数) |
[编辑] 条件变量
定义于头文件
<threads.h> | |
cnd_t
|
条件变量标识符 |
(C11) |
创建条件变量 (函数) |
(C11) |
解锁一个阻塞在条件变量上的线程 (函数) |
(C11) |
解锁所有阻塞在条件变量上的线程 (函数) |
(C11) |
阻塞在条件变量上 (函数) |
(C11) |
带超时时间阻塞在条件变量上 (函数) |
(C11) |
销毁条件变量 (函数) |
[编辑] 线程局部存储
定义于头文件
<threads.h> | |
(C11)(在 C23 中移除) |
存储类说明符 _Thread_local 的便捷宏 (关键字宏) |
tss_t
|
线程特定存储指针 |
(C11) |
析构函数被调用的最大次数 (宏常量) |
tss_dtor_t (C11) |
函数指针类型 void(*)(void*),用于 TSS 析构函数 (类型定义) |
(C11) |
使用给定的析构函数创建线程特定存储指针 (函数) |
(C11) |
从线程特定存储读取 (函数) |
(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 Atomics <stdatomic.h> (页码: 待定)
- 7.26 Threads <threads.h> (页码: 待定)
- 7.31.8 Atomics <stdatomic.h> (页码: 待定)
- 7.31.15 Threads <threads.h> (页码: 待定)
- C17 标准 (ISO/IEC 9899:2018)
- 7.17 Atomics <stdatomic.h> (页码: 200-209)
- 7.26 Threads <threads.h> (页码: 274-283)
- 7.31.8 Atomics <stdatomic.h> (页码: 332)
- 7.31.15 Threads <threads.h> (页码: 333)
- C11 标准 (ISO/IEC 9899:2011)
- 7.17 Atomics <stdatomic.h> (页码: 273-286)
- 7.26 Threads <threads.h> (页码: 376-387)
- 7.31.8 Atomics <stdatomic.h> (页码: 455-456)
- 7.31.15 Threads <threads.h> (页码: 456)
[编辑] 参见
C++ 文档 关于 并发支持库
|
[编辑] 外部链接
GNU GCC Libc 手册: ISO C 互斥锁 |