名稱空間
變體
操作

併發支援庫

來自 cppreference.com
< c

C 語言內建支援執行緒、原子操作、互斥體、條件變數和執行緒區域性儲存。

這些特性是可選提供的

  • 如果編譯器定義了宏常量 __STDC_NO_THREADS__,則標頭檔案 <threads.h> 及其提供的所有名稱將不被提供;
  • 如果編譯器定義了宏常量 __STDC_NO_ATOMICS__,則標頭檔案 <stdatomic.h> 及其提供的所有名稱將不被提供。

另請參閱 _Atomic 型別說明符和限定符

目錄

[編輯] 執行緒

在標頭檔案 <threads.h> 中定義
thrd_t 實現定義的完整物件型別,用於標識一個執行緒 [編輯]
建立執行緒
(函式) [編輯]
檢查兩個識別符號是否引用同一個執行緒
(函式) [編輯]
獲取當前執行緒識別符號
(函式) [編輯]
暫停呼叫執行緒的執行指定時間
(函式) [編輯]
讓出當前時間片
(函式) [編輯]
終止呼叫執行緒
(函式) [編輯]
分離執行緒
(函式) [編輯]
阻塞直到執行緒終止
(函式) [編輯]
指示執行緒錯誤狀態
(常量) [編輯]
thrd_start_t
(C11)
函式指標型別 int(*)(void*) 的 typedef,由 thrd_create 使用
(typedef) [編輯]

[編輯] 原子操作

在標頭檔案 <stdatomic.h> 中定義
原子型別上的操作
指示給定的原子型別是否是無鎖的
(宏常量) [編輯]
指示原子物件是否是無鎖的
(函式) [編輯]
在原子物件中儲存值
(函式) [編輯]
從原子物件中讀取值
(函式) [編輯]
與原子物件的值交換
(函式) [編輯]
如果舊值是期望的,則與原子物件交換值,否則讀取舊值
(函式) [編輯]
原子加法
(函式) [編輯]
原子減法
(函式) [編輯]
原子位或
(函式) [編輯]
原子位異或
(函式) [編輯]
原子位與
(函式) [編輯]
標誌型別和操作
無鎖原子布林標誌
(結構體)[編輯]
將 atomic_flag 設定為 true 並返回舊值
(函式) [編輯]
將 atomic_flag 設定為 false
(函式) [編輯]
初始化
初始化現有原子物件
(函式) [編輯]
(C11)(C17 中已棄用)(C23 中已移除)
初始化新的原子物件
(函式宏) [編輯]
初始化新的 atomic_flag
(宏常量) [編輯]
記憶體同步順序
定義記憶體排序約束
(列舉) [編輯]
memory_order_consume 打斷依賴鏈
(函式宏) [編輯]
通用記憶體排序依賴的屏障同步原語
(函式) [編輯]
執行緒與在同一執行緒中執行的訊號處理程式之間的屏障
(函式) [編輯]
便捷類型別名
型別定義名稱 完整型別名稱
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 解構函式
(typedef) [編輯]
使用給定解構函式建立執行緒區域性儲存指標
(函式) [編輯]
從執行緒區域性儲存讀取
(函式) [編輯]
寫入執行緒區域性儲存
(函式) [編輯]
釋放給定執行緒區域性指標持有的資源
(函式) [編輯]

[編輯] 保留識別符號

在 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> (p: TBD)
  • 7.26 執行緒 <threads.h> (p: TBD)
  • 7.31.8 原子操作 <stdatomic.h> (p: TBD)
  • 7.31.15 執行緒 <threads.h> (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.17 原子操作 <stdatomic.h> (p: 200-209)
  • 7.26 執行緒 <threads.h> (p: 274-283)
  • 7.31.8 原子操作 <stdatomic.h> (p: 332)
  • 7.31.15 執行緒 <threads.h> (p: 333)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.17 原子操作 <stdatomic.h> (p: 273-286)
  • 7.26 執行緒 <threads.h> (p: 376-387)
  • 7.31.8 原子操作 <stdatomic.h> (p: 455-456)
  • 7.31.15 執行緒 <threads.h> (p: 456)

[編輯] 另請參閱

C++ 文件,關於 併發支援庫

[編輯] 外部連結

GNU GCC Libc Manual: ISO C Mutexes