命名空間
變體
動作

並行支援函式庫

出自 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>
原子類型上的操作
指示給定的原子型別是否為免鎖的
(巨集常數) [編輯]
指示原子物件是否為免鎖的
(函數) [編輯]
將值儲存至原子物件中
(函數) [編輯]
從原子物件中讀取值
(函數) [編輯]
交換原子物件的值
(函數) [編輯]
若舊值符合預期則交換原子物件的值,否則讀取舊值
(函數) [編輯]
原子加法
(函數) [編輯]
原子減法
(函數) [編輯]
原子位元或 (OR)
(函數) [編輯]
原子位元互斥或 (XOR)
(函數) [編輯]
原子位元與 (AND)
(函數) [編輯]
旗標類型及操作
免鎖原子布林旗標
(struct)[編輯]
將 atomic_flag 設為 true 並返回舊值
(函數) [編輯]
將 atomic_flag 設為 false
(函數) [編輯]
初始化
初始化現有的原子物件
(函數) [編輯]
(C11)(C17 中棄用)(C23 中移除)
初始化新的原子物件
(函數巨集) [編輯]
初始化新的 atomic_flag
(巨集常數) [編輯]
記憶體同步順序
定義記憶體排序限制
(列舉) [編輯]
memory_order_consume 中斷依賴鏈
(函數巨集) [編輯]
通用記憶體順序相依的屏障 (fence) 同步原語
(函數) [編輯]
同一執行緒中執行緒與訊號處理常式之間的屏障 (fence)
(函數) [編輯]
便捷型別別名
Typedef 名稱 完整型別名稱
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 Atomics <stdatomic.h> (p: 待定)
  • 7.26 Threads <threads.h> (p: 待定)
  • 7.31.8 Atomics <stdatomic.h> (p: 待定)
  • 7.31.15 Threads <threads.h> (p: 待定)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.17 Atomics <stdatomic.h> (p: 200-209)
  • 7.26 Threads <threads.h> (p: 274-283)
  • 7.31.8 Atomics <stdatomic.h> (p: 332)
  • 7.31.15 Threads <threads.h> (p: 333)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.17 Atomics <stdatomic.h> (p: 273-286)
  • 7.26 Threads <threads.h> (p: 376-387)
  • 7.31.8 Atomics <stdatomic.h> (p: 455-456)
  • 7.31.15 Threads <threads.h> (p: 456)

[編輯] 參見

C++ 文件 關於 並發支援程式庫

[編輯] 外部連結

GNU GCC Libc 手冊:ISO C 互斥
English Deutsch 日本語 中文(简体) 中文(繁體)