<atomic>

定義類別和樣板類別用來建立支援基本作業的類型。

#include <atomic>

備註

注意事項注意事項

使用 /clr/clr:pure,以編譯的程式碼,這個標題會停止。

基本作業可以協助您使用多執行緒正確操作物件,而不需要使用 Mutex 鎖定的兩個主要屬性。

  • 由於基本作業不能被的,相同物件的第二部分完成從不同的執行緒可能在第一個基本作業前後只取得物件的狀態。

  • 根據其 memory_order 引數,基本作業在相同執行緒上建立其他基本作業的可視性的要求。 因此,它會禁止違反排序要求的編譯器最佳化。

在某些平台上,有效率地實作型別陣列的基本作業可能是不可能的,而不使用 mutex 鎖定。 如果該型別使用的基本作業不會鎖定,原子型別是 無鎖定 。

類別提供 atomic_flag 含有 bool 旗標的最小的原子型別。 其作業永遠是無鎖定。

樣板類別 atomic<Ty> 儲存其引數型別 Ty 物件並提供對該的原子存取儲存的值。 您可以將它具現化使用 memcmp,可以重複使用 memcpy 和用於相等測試中的任何型別。 特別是,您可以將它與符合這些需求和,在許多情況下,使用浮點型別的使用者定義型別。

範本也有一組整數型別的特製化和指標的部分特製化。 這些特製化提供透過主要樣板沒有可用的其他作業。

指標特製化

atomic<Ty *> 部分特製化適用於所有指標型別。 它們為指標算術提供方法。

整數特製化

atomic<integral> 特製化適用於所有整數型別。 它們提供透過主要樣板沒有可用的其他作業。

每個 atomic<integral> 型別具有您在 if directive 可以用來判斷在編譯時期的對應巨集在該型別的作業是否為無鎖定。 如果巨集的值為零,在型別的作業不是無鎖定。 如果值是一,作業可能是無鎖定,因此,需要執行階段檢查。 如果值是、,作業是無鎖定。 您可以使用函式 atomic_is_lock_free 判斷在執行階段是型別的作業是否為無鎖定。

對於每一個整數型別,則處理該整數型別物件的對應名為原子型別。 每個 atomic_integral 型別具有相同的一組成員函式。 atomic<Ty> 的對應的執行個體化,而且可以傳遞至任何非成員函式不可部分完成的作業。

atomic_integral 型別

整數類資料型別

atomic_is_lock_free 巨集

atomic_char

char

ATOMIC_CHAR_LOCK_FREE

atomic_schar

signed char

ATOMIC_CHAR_LOCK_FREE

atomic_uchar

unsigned char

ATOMIC_CHAR_LOCK_FREE

atomic_char16_t

char16_t

ATOMIC_CHAR16_T_LOCK_FREE

atomic_char32_t

char32_t

ATOMIC_CHAR32_T_LOCK_FREE

atomic_wchar_t

wchar_t

ATOMIC_WCHAR_T_LOCK_FREE

atomic_short

short

ATOMIC_SHORT_LOCK_FREE

atomic_ushort

unsigned short

ATOMIC_SHORT_LOCK_FREE

atomic_int

int

ATOMIC_INT_LOCK_FREE

atomic_uint

unsigned int

ATOMIC_INT_LOCK_FREE

atomic_long

long

ATOMIC_LONG_LOCK_FREE

atomic_ulong

unsigned long

ATOMIC_LONG_LOCK_FREE

atomic_llong

long long

ATOMIC_LLONG_LOCK_FREE

atomic_ullong

unsigned long long

ATOMIC_LLONG_LOCK_FREE

Typedef 名稱為不可部分完成樣板的特製化在標題 <inttypes.h>定義的某些存在的型別。

原子型別

Typedef 名稱

atomic_int8_t

atomic<int8_t>

atomic_uint8_t

atomic<uint8_t>

atomic_int16_t

atomic<int16_t>

atomic_uint16_t

atomic<uint16_t>

atomic_int32_t

atomic<int32_t>

atomic_uint32_t

atomic<uint32_t>

atomic_int64_t

atomic<int64_t>

atomic_uint64_t

atomic<uint64_t>

atomic_int_least8_t

atomic<int_least8_t>

atomic_uint_least8_t

atomic<uint_least8_t>

atomic_int_least16_t

atomic<int_least16_t>

atomic_uint_least16_t

atomic<uint_least16_t>

atomic_int_least32_t

atomic<int_least32_t>

atomic_uint_least32_t

atomic<uint_least32_t>

atomic_int_least64_t

atomic<int_least64_t>

atomic_uint_least64_t

atomic<uint_least64_t>

atomic_int_fast8_t

atomic<int_fast8_t>

atomic_uint_fast8_t

atomic<uint_fast8_t>

atomic_int_fast16_t

atomic<int_fast16_t>

atomic_uint_fast16_

atomic<uint_fast16_t>

atomic_int_fast32_t

atomic<int_fast32_t>

atomic_uint_fast32_t

atomic<uint_fast32_t>

atomic_int_fast64_t

atomic<int_fast64_t>

atomic_uint_fast64_t

atomic<uint_fast64_t>

atomic_intptr_t

atomic<intptr_t>

atomic_uintptr_t

atomic<uintptr_t>

atomic_size_t

atomic<size_t>

atomic_ptrdiff_t

atomic<ptrdiff_t>

atomic_intmax_t

atomic<intmax_t>

atomic_uintmax_t

atomic<uintmax_t>

Structs

名稱

描述

不可部分完成的結構

描述對儲存值的基本作業的物件。

atomic_flag 結構

描述原子集合和清除 bool 旗標的物件。

列舉

名稱

描述

memory_order 列舉

提供的符號名稱。在記憶體位置的同步處理作業。 這些作業會影響執行緒的工作方式變成可見在另一個。

功能

下列清單中,在 _explicit 不結束函式有對應的 _explicit的語意,不過,它們會 memory_order_seq_cst的隱含 memory_order 引數。

名稱

描述

atomic_compare_exchange_strong 函式

執行不可部分完成的 比較和交換的 作業。

atomic_compare_exchange_strong_explicit 函式

執行不可部分完成的 比較和交換的 作業。

atomic_compare_exchange_weak 函式

執行弱 原子比較和交換的 作業。

atomic_compare_exchange_weak_explicit 函式

執行弱 原子比較和交換的 作業。

atomic_exchange 函式

取代的儲存值。

atomic_exchange_explicit 函式

取代的儲存值。

atomic_fetch_add 函式

將指定的值加入至現有的儲存值。

atomic_fetch_add_explicit 函式

將指定的值加入至現有的儲存值。

atomic_fetch_and 函式

執行位元在某個值與現有的儲存值的 and 。

atomic_fetch_and_explicit 函式

執行位元在某個值與現有的儲存值的 and 。

atomic_fetch_or 函式

執行位元在某個值與現有的儲存值的 or 。

atomic_fetch_or_explicit 函式

執行位元在某個值與現有的儲存值的 or 。

atomic_fetch_sub 函式

從現有的儲存值減去某個值。

atomic_fetch_sub_explicit 函式

從現有的儲存值減去某個值。

atomic_fetch_xor 函式

執行位元在某個值與現有的儲存值的 exclusive or 。

atomic_fetch_xor_explicit 函式

執行位元在某個值與現有的儲存值的 exclusive or 。

atomic_flag_clear 函式

將一 atomic_flag 物件的旗標傳遞至 false。

atomic_flag_clear_explicit 函式

將一 atomic_flag 物件的旗標傳遞至 false。

atomic_flag_test_and_set 函式

將一 atomic_flag 物件的旗標傳遞至 true。

atomic_flag_test_and_set_explicit 函式

將一 atomic_flag 物件的旗標傳遞至 true。

atomic_init 函式

設定 atomic 物件的儲存值。

atomic_is_lock_free 函式

指定在指定物件的基本作業是否為無鎖定。

atomic_load 函式

擷取原子值。

atomic_load_explicit 函式

擷取原子值。

atomic_signal_fence 函式

做為在呼叫的執行緒會在柵欄之間的記憶體排序要求有訊號處理常式便會執行在相同執行緒上 柵欄 。

atomic_store 函式

儲存原子值。

atomic_store_explicit 函式

儲存原子值。

atomic_thread_fence 函式

為建立記憶體排序要求有關其他柵欄的 柵欄 。

kill_dependency 函式

中斷一個可能的相依性鏈結。

請參閱

參考

標準樣板程式庫

其他資源

標頭檔