并发支持库
来自cppreference.com
< c
C 提供线程、原子操作、互斥、条件变量及线程特定存储的内建支持。
这些特性是可选地提供的:
- 若编译器定义宏常量
__STDC_NO_THREADS__
,则不提供头文件 <threads.h> 及所有在其中提供的名称; - 若编译器定义宏常量
__STDC_NO_ATOMICS__
,则不提供头文件 <stdatomic.h> 及所有在其中提供的名称。
目录 |
[编辑]线程
在标头 <threads.h> 定义 | |
thrd_t | 实现定义的标识线程的完整对象类型 |
(C11) | 创建线程 (函数) |
(C11) | 检查两个标识符是否表示同一线程 (函数) |
(C11) | 获取当前线程标识符 (函数) |
(C11) | 在给定的时间段内挂起调用方线程的执行 (函数) |
(C11) | 让出当前时间片段 (函数) |
(C11) | 终止调用方线程 (函数) |
(C11) | 分离线程 (函数) |
(C11) | 阻塞到线程终止为止 (函数) |
指示线程错误状态 (常量) | |
thrd_start_t (C11) | 函数指针类型 int(*)(void*) 的 typedef,为 thrd_create 所用 (typedef) |
[编辑]原子操作
在标头 <stdatomic.h> 定义 | |
原子类型上的操作 | |
指示给定的原子类型为免锁 (宏常量) | |
(C11) | 指示原子对象是否免锁 (函数) |
存储值到原子对象 (函数) | |
从原子对象读取值 (函数) | |
将原子对象的值与一个值交换 (函数) | |
若原子对象的旧值为所期待的值则将之与一个值交换,否则读取该旧值 (函数) | |
原子加法 (函数) | |
原子减法 (函数) | |
原子逐位或(OR) (函数) | |
原子逐位异或(XOR) (函数) | |
原子逐位与(AND) (函数) | |
标志类型及操作 | |
(C11) | 免锁原子布尔标志 (结构体) |
设置 atomic_flag 为 true 并返回旧值 (函数) | |
设置 atomic_flag 为 false (函数) | |
初始化 | |
(C11) | 初始化既存的原子对象 (函数) |
(C11)(C17 弃用)(C23 移除) | 初始化新的原子对象 (宏函数) |
(C11) | 初始化新的 atomic_flag (宏常量) |
内存同步顺序 | |
(C11) | 定义内存顺序制约 (枚举) |
(C11) | 打破 memory_order_consume 的依赖链 (宏函数) |
(C11) | 通用的内存顺序依赖的栅栏同步原语 (函数) |
(C11) | 线程与执行于同一线程的信号处理函数间的栅栏 (函数) |
便利类型别名 | |
typedef 名 | 完整类型名 |
atomic_bool (C11) | _Atomic _Bool(C23 前)_Atomic bool(C23 起) |
atomic_char (C11) | _Atomic char |
atomic_schar (C11) | _Atomic signedchar |
atomic_uchar (C11) | _Atomic unsignedchar |
atomic_short (C11) | _Atomic short |
atomic_ushort (C11) | _Atomic unsignedshort |
atomic_int (C11) | _Atomic int |
atomic_uint (C11) | _Atomic unsignedint |
atomic_long (C11) | _Atomic long |
atomic_ulong (C11) | _Atomic unsignedlong |
atomic_llong (C11) | _Atomic longlong |
atomic_ullong (C11) | _Atomic unsignedlonglong |
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 析构器 (typedef) |
(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> (第 TBD 页)
- 7.26 Threads <threads.h> (第 TBD 页)
- 7.31.8 Atomics <stdatomic.h> (第 TBD 页)
- 7.31.15 Threads <threads.h> (第 TBD 页)
- 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 Manual: ISO C Mutexes |