接著上節(jié) mutex限番,本節(jié)主要介紹atomic的內(nèi)容,練習(xí)代碼地址蛀蜜。本文參考http://www.cplusplus.com/reference/atomic/和http://en.cppreference.com/w/cpp/header/atomic學(xué)習(xí)特碳。
一诚亚、<atomic>頭文件結(jié)構(gòu)
<atomic>頭文件:原子類型是封裝了一個值的類型,它的訪問保證不會導(dǎo)致數(shù)據(jù)的競爭午乓,并且可以用于在不同的線程之間同步內(nèi)存訪問站宗。
這個頭聲明了兩個c++類,原子和atomic_flag益愈,它實現(xiàn)了自包含類中的原子類型的所有特性梢灭。header還聲明了整個C樣式類型和函數(shù)與C中的原子支持兼容。頭文件中結(jié)構(gòu)定義查看蒸其。
<atomic>頭文件的結(jié)構(gòu):
class | |
---|---|
atomic | 用于bool敏释、整數(shù)和指針類型的原子類模板和特殊化 (類模板) |
atomic_flag | 無鎖布爾原子類型(類) |
Types | |
---|---|
memory_order | 為給定的原子操作定義內(nèi)存排序約束(typedef) |
Typedefs | |
---|---|
std::atomic_bool | std::atomic<bool> |
std::atomic_char | std::atomic<char> |
std::atomic_schar | std::atomic<signed char> |
std::atomic_uchar | std::atomic<unsigned char> |
std::atomic_short | std::atomic<short> |
std::atomic_ushort | std::atomic<unsigned short> |
std::atomic_int | std::atomic<int> |
std::atomic_uint | std::atomic<unsigned int> |
std::atomic_long | std::atomic<long> |
std::atomic_ulong | std::atomic<unsigned long> |
std::atomic_llong | std::atomic<long long> |
std::atomic_ullong | std::atomic<unsigned long long> |
std::atomic_char16_t | std::atomic<char16_t> |
std::atomic_char32_t | std::atomic<char32_t> |
std::atomic_wchar_t | std::atomic<wchar_t> |
std::atomic_int_least8_t | std::atomic<int_least8_t> |
std::atomic_uint_least8_t | std::atomic<uint_least8_t> |
std::atomic_int_least16_t | std::atomic<int_least16_t> |
std::atomic_uint_least16_t | std::atomic<uint_least16_t> |
std::atomic_int_least32_t | std::atomic<int_least32_t> |
std::atomic_uint_least32_t | std::atomic<uint_least32_t> |
std::atomic_int_least64_t | std::atomic<int_least64_t> |
std::atomic_uint_least64_t | std::atomic<uint_least64_t> |
std::atomic_int_fast8_t | std::atomic<int_fast8_t> |
std::atomic_uint_fast8_t | std::atomic<uint_fast8_t> |
std::atomic_int_fast16_t | std::atomic<int_fast16_t> |
std::atomic_uint_fast16_t | std::atomic<uint_fast16_t> |
std::atomic_int_fast32_t | std::atomic<int_fast32_t> |
std::atomic_uint_fast32_t | std::atomic<uint_fast32_t> |
std::atomic_int_fast64_t | std::atomic<int_fast64_t> |
std::atomic_uint_fast64_t | std::atomic<uint_fast64_t> |
std::atomic_intptr_t | std::atomic<intptr_t> |
std::atomic_uintptr_t | std::atomic<uintptr_t> |
std::atomic_size_t | std::atomic<size_t> |
std::atomic_ptrdiff_t | std::atomic<ptrdiff_t> |
std::atomic_intmax_t | std::atomic<intmax_t> |
std::atomic_uintmax_t | std::atomic<uintmax_t> |
Functions | |
---|---|
kill_dependency | 從std::memory_order_consume依賴樹中刪除指定對象 (模版函數(shù)) |
atomic_thread_fence | 通用內(nèi)存順序依賴關(guān)系的隔離同步原語(函數(shù)) |
atomic_signal_fence | 在同一線程中執(zhí)行的線程和信號處理程序之間的隔離(函數(shù)) |
Functions for atomic objects (C-style) | description |
---|---|
atomic_is_lock_free | 檢查原子類型的操作是否免鎖的(函數(shù)模板) |
atomic_store </br> atomic_store_explicit | 使用非原子參數(shù)以原子方式替換原子對象的值(函數(shù)模板) |
atomic_load </br> atomic_load_explicit | 在原子對象中原子性地獲取存儲的值(函數(shù)模板) |
atomic_exchange </br> atomic_exchange_explicit | 原子性地用非原子參數(shù)替換原子對象的值,并返回原子的舊值摸袁。(函數(shù)模板) |
atomic_compare_exchange_weak </br> atomic_compare_exchange_weak_explicit </br> atomic_compare_exchange_strong </br> atomic_compare_exchange_strong_explicit | 原子地比較原子對象和非原子參數(shù)的值钥顽,如果不相等,則執(zhí)行原子交換但惶,如果沒有耳鸯,就load atomic |
atomic_fetch_add </br> atomic_fetch_add_explicit | 向原子對象添加非原子值湿蛔,并獲取原子的前值(函數(shù)模板) |
atomic_fetch_sub </br> atomic_fetch_sub_explicit | 從原子對象中減去非原子值膀曾,并獲得原子的前值(函數(shù)模板) |
atomic_fetch_and </br> atomic_fetch_and_explicit | 用邏輯結(jié)果和非原子參數(shù)替換原子對象,并獲得原子的前值(函數(shù)模板) |
atomic_fetch_or </br> atomic_fetch_or_explicit | 用邏輯或非原子參數(shù)替換原子對象阳啥,并獲得原子的前值(函數(shù)模板) |
atomic_fetch_xor atomic_fetch_xor_explicit |
用邏輯XOR和非原子參數(shù)替換原子對象添谊,并獲得原子的前值(函數(shù)模板) |
Functions for atomic flags (C-style) | description |
---|---|
atomic_flag_test_and_set</br>atomic_flag_test_and_set_explicit | 原子地將flag設(shè)置為true并返回其先前的值 (函數(shù)) |
atomic_flag_clear</br>atomic_flag_clear_explicit | 原子地將flag設(shè)置成false(函數(shù)) |
Preprocessor macros | description |
---|---|
ATOMIC_VAR_INIT | 靜態(tài)存儲時間的原子變量的常量初始化(宏) |
ATOMIC_FLAG_INIT | 初始化 std::atomic_flag為 false |
Macro constants
// lock-free property
#define ATOMIC_BOOL_LOCK_FREE /*unspecified*/
#define ATOMIC_CHAR_LOCK_FREE /*unspecified*/
#define ATOMIC_CHAR16_T_LOCK_FREE /*unspecified*/
#define ATOMIC_CHAR32_T_LOCK_FREE /*unspecified*/
#define ATOMIC_WCHAR_T_LOCK_FREE /*unspecified*/
#define ATOMIC_SHORT_LOCK_FREE /*unspecified*/
#define ATOMIC_INT_LOCK_FREE /*unspecified*/
#define ATOMIC_LONG_LOCK_FREE /*unspecified*/
#define ATOMIC_LLONG_LOCK_FREE /*unspecified*/
#define ATOMIC_POINTER_LOCK_FREE /*unspecified*/
上面是對<atomic>頭文件結(jié)構(gòu)的描述;下面具體按照上面的結(jié)構(gòu)分析:
二 察迟、std::atomic
定義 : //類模版
template< class T > struct atomic; (1) (since C++11)
template<> struct atomic<Integral>; (2) (since C++11)
template<> struct atomic<bool>; (3) (since C++11)
template< class T > struct atomic<T*>; (4) (since C++11) 指針特化
std::atomic模板的每個實例化和專門化都定義了一個原子類型斩狱。如果一個線程在另一個線程讀取它時寫入一個原子對象耳高,那么行為就會被明確定義(參見關(guān)于數(shù)據(jù)競爭的詳細信息的內(nèi)存模型)。此外所踊,對原子對象的訪問可以建立線程間的同步泌枪,并按照std::memoryorder指定非原子性的內(nèi)存訪問。
std::atomic可以用任何簡單的可復(fù)制的t實例化秕岛。同時std::atomic是不可復(fù)制的碌燕,也不是可移動的。
Member functions
atomic() noexcept = default;(1) default (since C++11)
constexpr atomic( T desired ) noexcept;(2) initialization (since C++11)
atomic( const atomic& ) = delete;(3) copy [deleted] (since C++11)
構(gòu)造新的原子變量继薛。
1)將原子對象放在未初始化的狀態(tài)中修壕。一個未初始化的原子對象可以通過調(diào)用atomicinit來初始化卡睦。
2)用desired 初始化對象谦屑。初始化不是原子性的医清。
3)原子變量不是可復(fù)制的雁刷。
示例1:
// constructing atomics
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread> // std::thread, std::this_thread::yield
#include <vector> // std::vector
std::atomic<bool> ready (false);
std::atomic_flag winner = ATOMIC_FLAG_INIT;
void count1m (int id) {
while (!ready) { std::this_thread::yield(); } // wait for the ready signal
for (volatile int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.test_and_set()) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
T operator=( T desired ) noexcept; set value (1)
T operator=( T desired ) volatile noexcept; set value (1)
atomic& operator=( const atomic& ) = delete; copy [deleted] (2)
atomic& operator=( const atomic& ) volatile = delete; copy [deleted] (2)
1 用val替換存儲的值士骤。該操作是原子性的割捅,并使用順序一致性(memoryorderseqcst)酸纲。要使用不同的內(nèi)存排序來修改值组哩,請參見atomic::store咖楣。
2 原子對象沒有定義的復(fù)制賦值壶冒,但是注意它們是可以隱式地轉(zhuǎn)換為類型T。
注意:與大多數(shù)賦值運算符不同截歉,原子類型的賦值運算符不會返回對它們的左參數(shù)的引用胖腾。它們返回一個存儲值的副本。
示例2:
// atomic::operator=/operator T example:
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread, std::this_thread::yield
std::atomic<int> foo(0);
void set_foo(int x) {
foo = x;
}
void print_foo() {
while (foo==0) { // wait while foo=0
std::this_thread::yield();
}
std::cout << "foo: " << foo << '\n';
}
int main ()
{
std::thread first (print_foo);
std::thread second (set_foo,10);
first.join();
second.join();
return 0;
}
General atomic operations
bool is_lock_free() const noexcept;
bool is_lock_free() const volatile noexcept;
一個無鎖對象并不會導(dǎo)致其他線程在訪問時被阻塞(可能使用某種類型的事務(wù)性內(nèi)存)瘪松。該函數(shù)返回的值與相同類型的所有其他對象返回的值一致咸作。
檢查這個類型的所有對象的原子操作是否都是無鎖的。返回true表示lock_free.
示例3:
#include <iostream>
#include <utility>
#include <atomic>
struct A { int a[100]; };
struct B { int x, y; };
int main()
{
std::cout << std::boolalpha
<< "std::atomic<A> is lock free? "
<< std::atomic<A>{}.is_lock_free() << '\n'
<< "std::atomic<B> is lock free? "
<< std::atomic<B>{}.is_lock_free() << '\n';
return 0;
}
void store (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
void store (T val, memory_order sync = memory_order_seq_cst) noexcept;
用val替換包含的值宵睦。操作是原子的记罚,按照同步所指定的內(nèi)存順序內(nèi)存數(shù)序包括(std::memory_order_relaxed, std::memory_order_release 和std::memory_order_seq_cst)。
參數(shù)sync的描述(后續(xù)會介紹memory_order):
memory_order_relaxed: 不同步副作用壳嚎。
memory_order_release:同步下一個使用或者獲取操作的副作用桐智。
memory_order_seq_cst:同步所有與其他順序一致操作的可見的副作用,并遵循一個完整的順序烟馅。
示例4:
// atomic::load/store example
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::memory_order_relaxed
#include <thread> // std::thread
std::atomic<int> foo (0);
void set_foo(int x) {
foo.store(x,std::memory_order_relaxed); // set value atomically
}
void print_foo() {
int x;
do {
x = foo.load(std::memory_order_relaxed); // get value atomically
} while (x==0);
std::cout << "foo: " << x << '\n';
}
int main ()
{
std::thread first (print_foo);
std::thread second (set_foo,10);
first.join();
second.join();
return 0;
}
T load (memory_order sync = memory_order_seq_cst) const volatile noexcept;
T load (memory_order sync = memory_order_seq_cst) const noexcept;
返回包含值说庭。操作是原子的,按照同步所指定的內(nèi)存順序郑趁。指令必須是std::memory_order_relaxed, std::memory_order_consume, std::memory_order_acquire 和 std::memory_order_seq_cst)刊驴;否則,行為是沒有定義的。
sync指令描述:
上文已經(jīng)描述了std::memory_order_relaxed和 std::memory_order_seq_cst捆憎,這里只描述memory_order_acquire和memory_order_consume舅柜。
memory_order_acquire:同步從最后一個Release或順序一致的操作所有可見的副作用。
memory_order_consume:同步與最后一個Release或順序一致的操作所產(chǎn)生的依賴關(guān)系的可見的副作用躲惰。
示例5:
// atomic::load/store example
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::memory_order_relaxed
#include <thread> // std::thread
std::atomic<int> foo (0);
void set_foo(int x) {
foo.store(x,std::memory_order_relaxed); // set value atomically
}
void print_foo() {
int x;
do {
x = foo.load(std::memory_order_relaxed); // get value atomically
} while (x==0);
std::cout << "foo: " << x << '\n';
}
int main ()
{
std::thread first (print_foo);
std::thread second (set_foo,10);
first.join();
second.join();
return 0;
}
operator T() const volatile noexcept;
operator T() const noexcept;
這是一個類型轉(zhuǎn)換的操作符:這個表達式期望它包含的類型(T)的值致份,調(diào)用這個成員函數(shù),訪問包含的值础拨。
該操作是原子的知举,并使用順序一致性(memory_order_seq_cst)。要檢索具有不同內(nèi)存順序的值太伊,相當于std::atomic::load雇锡。
示例6:
// atomic::operator=/operator T example:
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread, std::this_thread::yield
std::atomic<int> foo(0);
std::atomic<int> bar(0);
void set_foo(int x) {
foo = x;
}
void copy_foo_to_bar () {
while (foo==0) std::this_thread::yield();
bar = static_cast<int>(foo);
}
void print_bar() {
while (bar==0) std::this_thread::yield();
std::cout << "bar: " << bar << '\n';
}
int main ()
{
std::thread first (print_bar);
std::thread second (set_foo,10);
std::thread third (copy_foo_to_bar);
first.join();
second.join();
third.join();
return 0;
}
T exchange (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T exchange (T val, memory_order sync = memory_order_seq_cst) noexcept;
訪問和修改包含的值
用val替換所包含的值,并返回它之前的值僚焦。整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的那一刻之間锰提,值不會受到其他線程的影響。
sync指令描述:
上文已經(jīng)描述了std::memory_order_relaxed和 std::memory_order_seq_cst 和memory_order_consume 和 memory_order_acquire芳悲,這里只描述memory_order_acq_rel立肘。
memory_order_acq_rel:讀取作為一個獲取操作,并作為一個發(fā)布操作寫入名扛。
示例 7:
// atomic::exchange example
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector
std::atomic<bool> ready (false);
std::atomic<bool> winner (false);
void count1m (int id) {
while (!ready) {} // wait for the ready signal
for (int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.exchange(true)) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
bool compare_exchange_weak (T& expected, T val,
memory_order sync = memory_order_seq_cst) volatile noexcept; (1)
bool compare_exchange_weak (T& expected, T val,
memory_order sync = memory_order_seq_cst) noexcept; (1)
bool compare_exchange_weak (T& expected, T val,
memory_order success, memory_order failure) volatile noexcept; (2)
bool compare_exchange_weak (T& expected, T val,
memory_order success, memory_order failure) noexcept; (2)
bool compare_exchange_strong (T& expected, T val,
memory_order sync = memory_order_seq_cst) volatile noexcept; (1)
bool compare_exchange_strong (T& expected, T val,
memory_order sync = memory_order_seq_cst) noexcept; (1)
bool compare_exchange_strong (T& expected, T val,
memory_order success, memory_order failure) volatile noexcept; (2)
bool compare_exchange_strong (T& expected, T val,
memory_order success, memory_order failure) noexcept; (2)
* 比較原子對象的包含值與預(yù)期的內(nèi)容:
--1 如果是真的谅年,它會用val替換包含的值(比如存儲)。
--2 如果是假的肮韧,它會用所包含的值替換預(yù)期,因此調(diào)用該函數(shù)之后融蹂,如果被該原子對象封裝的值與參數(shù) expected 所指定的值不相等,expected 中的內(nèi)容就是原子對象的舊值弄企。
* 函數(shù)總是訪問包含的值來讀取它超燃,如果這個比較是真的,那么它也會替換它拘领。但是整個操作都是原子性的:在讀取值和被替換的時刻之間意乓,它的值不能被其他線程修改。
* 在第(2)種情況下约素,內(nèi)存序(Memory Order)的選擇取決于比較操作結(jié)果届良,如果比較結(jié)果為 true(即原子對象的值等于 expected),則選擇參數(shù) success 指定的內(nèi)存序圣猎,否則選擇參數(shù) failure 所指定的內(nèi)存序士葫。
** 注意:
這個函數(shù)直接的比較物理內(nèi)容所包含的價值與預(yù)期的內(nèi)容,這可能導(dǎo)致得到使用operator==比較的結(jié)果是一個失敗的結(jié)果,因為對象底層的物理內(nèi)容中可能存在位對齊或其他邏輯表示相同但是物理表示不同的值(比如 true 和 2 或 3,它們在邏輯上都表示"真"样漆,但在物理上兩者的表示并不相同)为障。
不像 compare_exchange_strong晦闰,這個弱版本允許(spuriously 地)返回 false(即原子對象所封裝的值與參數(shù) expected 的物理內(nèi)容相同放祟,但卻仍然返回 false)鳍怨,即使在預(yù)期的實際情況與所包含的對象相比較時也是如此。對于某些循環(huán)算法來說跪妥,這可能是可接受的行為鞋喇,并且可能會在某些平臺上帶來顯著的性能提升。在這些虛假的失敗中眉撵,函數(shù)返回false侦香,而不修改預(yù)期。
對于非循環(huán)算法來說纽疟, compare_exchange_strong通常是首選罐韩。
示例8:
// atomic::compare_exchange_weak example:
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector
// a simple global linked list:
struct Node { int value; Node* next; };
std::atomic<Node*> list_head (nullptr);
void append (int val) { // append an element to the list
Node* oldHead = list_head;
Node* newNode = new Node {val,oldHead};
// what follows is equivalent to: list_head = newNode, but in a thread-safe way:
while (!list_head.compare_exchange_weak(oldHead,newNode))
newNode->next = oldHead;
}
int main ()
{
// spawn 10 threads to fill the linked list:
std::vector<std::thread> threads;
for (int i=0; i<10; ++i) threads.push_back(std::thread(append,i));
for (auto& th : threads) th.join();
// print contents:
for (Node* it = list_head; it!=nullptr; it=it->next)
std::cout << ' ' << it->value;
std::cout << '\n';
// cleanup:
Node* it; while (it=list_head) {list_head=it->next; delete it;}
return 0;
}
compare_exchange_strong 跟 compare_exchange_week 不同的是:
與compare_exchange_weak 不同, strong版本的 compare-and-exchange 操作不允許(spuriously 地)返回 false,即原子對象所封裝的值與參數(shù) expected 的物理內(nèi)容相同污朽,比較操作一定會為 true散吵。不過在某些平臺下,如果算法本身需要循環(huán)操作來做檢查蟆肆, compare_exchange_weak 的性能會更好矾睦。
特定的操作支持(整形和指針)
if T is integral (1)
T fetch_add (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_add (T val, memory_order sync = memory_order_seq_cst) noexcept;
T fetch_sub (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_sub (T val, memory_order sync = memory_order_seq_cst) noexcept;
if T is pointer (2)
T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
*將val加或者減去到包含的值并返回在操作之前的值。
*整個操作是原子的(一個原子的讀-修改-寫操作):當在這個函數(shù)被修改的時候,讀取的(返回)值被讀取炎功,值不受其他線程的影響枚冗。
*這個成員函數(shù)是對整數(shù)(1)和指針(2)類型(除了bool除外)的原子專門化中定義。
*如果第二個參數(shù)使用默認值蛇损,則該函數(shù)等價于原子::運算符+ =赁温。
示例9:
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<long long> data;
void do_work()
{
data.fetch_add(1, std::memory_order_relaxed);
}
int main()
{
std::thread th1(do_work);
std::thread th2(do_work);
std::thread th3(do_work);
std::thread th4(do_work);
std::thread th5(do_work);
th1.join();
th2.join();
th3.join();
th4.join();
th5.join();
std::cout << "Result:" << data << '\n';
return 0;
}
T fetch_and (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_and (T val, memory_order sync = memory_order_seq_cst) noexcept;
T fetch_or (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_or (T val, memory_order sync = memory_order_seq_cst) noexcept;
T fetch_xor (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_xor (T val, memory_order sync = memory_order_seq_cst) noexcept;
* 讀取包含的值并替換調(diào)該值和val執(zhí)行一個位和操作的結(jié)果。
* 整個操作是原子的(一個原子的讀-修改-寫操作):當在這個函數(shù)被修改的時候,讀取的(返回)值被讀取淤齐,值不受其他線程的影響束世。
* 這個成員函數(shù)只在原子專門化中定義為整數(shù)類型(除了bool)。
* 如果第二個參數(shù)使用默認值床玻,則該函數(shù)等價于原子::operator& =毁涉。
-
std::atomic::operator++/std::atomic::operator--
pre-increment (1)
T operator++() volatile noexcept;
T operator++() noexcept;
T operator--() volatile noexcept;
T operator--() noexcept;
post-increment (2)
T operator++ (int) volatile noexcept;
T operator++ (int) noexcept;
T operator-- (int) volatile noexcept;
T operator-- (int) noexcept;
* 包含值的值進行增加或減少1,操作(1)返回所得到的包含值锈死,操作(2)返回之前的值贫堰。
* 整個操作是原子的(一個原子的讀-修改-寫操作):當在這個函數(shù)被修改的時候,讀取的(返回)值被讀取,值不受其他線程的影響待牵。
* 函數(shù)只在原子專門化中定義為整數(shù)和指針類型(除了bool)其屏。
* 這個函數(shù)的行為就像調(diào)用std::stomic::fetch_add(1)
,memory_order_seq_cst
作為參數(shù)缨该。
if T is integral (1)
T operator+= (T val) volatile noexcept;
T operator+= (T val) noexcept;
T operator-= (T val) volatile noexcept;
T operator-= (T val) noexcept;
T operator&= (T val) volatile noexcept;
T operator&= (T val) noexcept;
T operator|= (T val) volatile noexcept;
T operator|= (T val) noexcept;
T operator^= (T val) volatile noexcept;
T operator^= (T val) noexcept;
if T is pointer (2)
T operator+= (ptrdiff_t val) volatile noexcept;
T operator+= (ptrdiff_t val) noexcept;
T operator-= (ptrdiff_t val) volatile noexcept;
T operator-= (ptrdiff_t val) noexcept;
* 對于整形(1)和指針(2)類型的原子專門化是支持復(fù)合賦值的;每一個函數(shù)都訪問包含的值蛤袒,應(yīng)用合適的操作符妙真,并在操作之前返回包含值的值;所有這些操作都不會受到其他線程的影響珍德。
* 這些函數(shù)的行為就像使用memory_order_seq_cst調(diào)用std::stomic::fetch_ *函數(shù)一樣:
二 薄料、atomic_flag
atomic_flag是一個原子布爾類型都办。不同于std::atomic的所有專門化琳钉,它保證是lock_free溯壶。不像std::stomic< bool >验烧,std::atomic_flag不提供負載或存儲操作慨蓝。
示例10:
#include <thread>
#include <vector>
#include <iostream>
#include <atomic>
std::atomic_flag lock = ATOMIC_FLAG_INIT;
void f(int n)
{
for (int cnt = 0; cnt < 100; ++cnt) {
while (lock.test_and_set(std::memory_order_acquire)) // acquire lock
; // spin
std::cout << "Output from thread " << n << '\n';
lock.clear(std::memory_order_release); // release lock
}
}
int main()
{
std::vector<std::thread> v;
for (int n = 0; n < 10; ++n) {
v.emplace_back(f, n);
}
for (auto& t : v) {
t.join();
}
return 0;
}
atomic_flag() noexcept = default;
atomic_flag (const atomic_flag&T) = delete;
* atomic_flag在構(gòu)建(或設(shè)置或清除)上處于一個未指定的狀態(tài)此熬,除非它被顯式地初始化為ATOMIC_FLAG_INIT扶关。
* ATOMIC_FLAG_INIT初始化是通過簡單地調(diào)用默認構(gòu)造函數(shù)或其他方法來實現(xiàn)的鲫寄,這取決于特定的庫實現(xiàn)戳玫。
* atomic_flag值不能復(fù)制/移動蜡秽。
*** 注意: std::atomic_flag::operator=
不可賦值,其賦值操作符被刪除。。
示例11:
// constructing atomics: atomic<bool> vs atomic_flag
#include <iostream> // std::cout
#include <atomic> // std::atomic, std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread> // std::thread, std::this_thread::yield
#include <vector> // std::vector
std::atomic<bool> ready (false); // can be checked without being set
std::atomic_flag winner = ATOMIC_FLAG_INIT; // always set when checked
void count1m (int id) {
while (!ready) { std::this_thread::yield(); } // wait for the ready signal
for (int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.test_and_set()) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
bool test_and_set (memory_order sync = memory_order_seq_cst) volatile noexcept;
bool test_and_set (memory_order sync = memory_order_seq_cst) noexcept;
* 設(shè)置atomic_flag并返回是否在調(diào)用之前已經(jīng)設(shè)置的攀甚。
* * 整個操作是原子的(一個原子的讀-修改-寫操作):當在這個函數(shù)被修改的時候,讀取的(返回)值被讀取云稚,值不受其他線程的影響燕雁。
示例12:
// atomic_flag as a spinning lock
#include <iostream> // std::cout
#include <atomic> // std::atomic_flag
#include <thread> // std::thread
#include <vector> // std::vector
#include <sstream> // std::stringstream
std::atomic_flag lock_stream = ATOMIC_FLAG_INIT;
std::stringstream stream;
void append_number(int x) {
while (lock_stream.test_and_set()) {}
stream << "thread #" << x << '\n';
lock_stream.clear();
}
int main ()
{
std::vector<std::thread> threads;
for (int i=1; i<=10; ++i) threads.push_back(std::thread(append_number,i));
for (auto& th : threads) th.join();
std::cout << stream.str();
return 0;
}
void clear (memory_order sync = memory_order_seq_cst) volatile noexcept;
void clear (memory_order sync = memory_order_seq_cst) noexcept;
清除atomic_flag(即把atomic_flag 設(shè)為假)僧免。
清除atomic_flag使下一次調(diào)用成員atomic_flag::test_and_set對象返回false懂衩。
操作是原子的,按照sync所指定的內(nèi)存順序法希。
示例13:
// atomic_flag as a spinning lock
#include <iostream> // std::cout
#include <atomic> // std::atomic_flag
#include <thread> // std::thread
#include <vector> // std::vector
#include <sstream> // std::stringstream
std::atomic_flag lock_stream = ATOMIC_FLAG_INIT;
std::stringstream stream;
void append_number(int x) {
while (lock_stream.test_and_set()) {}
stream << "thread #" << x << '\n';
lock_stream.clear();
}
int main ()
{
std::vector<std::thread> threads;
for (int i=1; i<=10; ++i) threads.push_back(std::thread(append_number,i));
for (auto& th : threads) th.join();
std::cout << stream.str();
return 0;
}
三 、memory_order
作為用于執(zhí)行原子操作的函數(shù)的參數(shù)屋剑,用于指定如何同步不同線程上的其他操作。也可參見網(wǎng)址肄鸽。
定義:
```c
typedef enum memory_order {
memory_order_relaxed, // relaxed
memory_order_consume, // consume
memory_order_acquire, // acquire
memory_order_release, // release
memory_order_acq_rel, // acquire/release
memory_order_seq_cst // sequentially consistent
} memory_order;
* 當多個線程訪問原子對象時,所有原子操作都會對一個原子對象定義良好的行為:在任何其他原子操作能夠訪問該對象之前,每個原子操作都是完全在對象上執(zhí)行的梅鹦。這保證了這些對象上沒有數(shù)據(jù)競爭齐唆,而這正是定義原子性的特性茉帅。
* 但是,每個線程可能在內(nèi)存位置上執(zhí)行操作,而不是原子對象本身:這些操作可能會對其他線程產(chǎn)生可見的副作用昨凡。這種類型的參數(shù)允許指定操作的內(nèi)存順序子房,以確定這些(可能非原子)可見的副作用是如何在線程間同步的田度,使用原子操作作為同步點:
-
memory_order_relaxed
該操作在某一時刻被命令進行原子化送讲。這是最寬松的內(nèi)存順序监右,無法保證對不同線程的內(nèi)存訪問是如何根據(jù)原子操作進行排序的。
標記為memory_order_relaxed的原子操作不是同步操作;它們不會在并發(fā)內(nèi)存訪問中強制執(zhí)行順序扣癣。它們只保證原子性和修改順序的一致性授药。
示例14:
#include <vector>
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<int> cnt = {0};
void f()
{
for (int n = 0; n < 1000; ++n) {
cnt.fetch_add(1, std::memory_order_relaxed);
}
}
int main()
{
std::vector<std::thread> v;
for (int n = 0; n < 10; ++n) {
v.emplace_back(f);
}
for (auto& t : v) {
t.join();
}
std::cout << "Final counter value is " << cnt << '\n';
return 0;
}
-
memory_order_consume
如果在這個對釋放操作進行依賴(并且對加載線程有明顯的副作用)的釋放線程上 所有訪問內(nèi)存 已經(jīng)發(fā)生耕赘,那么操作將被命令執(zhí)行赚窃。
帶有這個內(nèi)存順序的加載操作在受影響的內(nèi)存位置上執(zhí)行獲取操作:當前線程依賴于當前裝載的值是掰,在此負載之前匾七,不需要讀取或?qū)懭搿T诋斍熬€程中可以看到相同的原子變量拢驾。在大多數(shù)平臺上,這只會影響編譯器優(yōu)化(參見下面的Release-Consume)鳖昌。 -
memory_order_acquire
在釋放線程(對加載線程有明顯的副作用)的情況下,操作被命令進行一次俐银。
帶有這個內(nèi)存順序的加載操作在受影響的內(nèi)存位置上執(zhí)行獲取操作:在此負載之前陪捷,在當前線程中不可以重新排序。所有在其他線程中都可以在當前線程中看到相同的原子變量(參見下面的Release-Acquire) -
memory_order_release
* 該操作被命令在消費或獲取操作之前發(fā)生烁涌,作為對內(nèi)存的其他訪問的同步點苍碟,可能對加載線程有可見的副作用。
* 具有這種內(nèi)存順序的存儲操作執(zhí)行釋放操作:在當前線程中撮执,在該存儲之后微峰,不可以在當前線程中重新排序。當前線程中所有的寫操作都可以在其他線程中看到抒钱,這些線程獲得相同的原子變量(參見下面的Release-Acquire)蜓肆,并且在使用相同原子的其他線程中,將對原子變量的依賴變?yōu)榭梢?參見下面的Release-Consume)谋币。 -
memory_order_acq_rel
* 該操作加載并存儲釋放(如上所述仗扬,用于memory_order_acquire 蕾额、memory_order_release)早芭。 -
memory_order_seq_cst
* 這個操作是按順序一致的方式排序的:所有使用這個內(nèi)存順序的操作都是在所有可能對其他線程都有可見副作用的內(nèi)存中執(zhí)行的。
* 這是最嚴格的內(nèi)存順序诅蝶,在非原子內(nèi)存訪問的情況下退个,保證了線程交互中最不意外的副作用募壕。
* 對于消費和獲取負載,順序一致的存儲操作被認為是發(fā)布操作语盈。
四舱馅、Functions
template <class T>
T kill_dependency (T y) noexcept;
* 返回y的值而不需要依賴項。
* 使用memory_order_consume 作為內(nèi)存順序的原子操作刀荒,要求編譯器檢查通過訪問存儲的內(nèi)存位置所帶來的依賴關(guān)系习柠。同步這樣的依賴關(guān)系可能會導(dǎo)致某些硬件設(shè)置被設(shè)置,并迫使編譯器放棄涉及這些內(nèi)存位置的某些潛在優(yōu)化照棋。
* 調(diào)用此函數(shù)指示編譯器资溃,任何依賴于y的依賴項都不應(yīng)被傳遞到返回值,而不需要同步烈炭。
-
atomic_thread_fence
"C"風(fēng)格導(dǎo)出的函數(shù):
extern "C" void atomic_thread_fence (memory_order sync) noexcept;
建立一個多線程的隔離:對這個函數(shù)的調(diào)用的時間點變成一個獲取或者一個釋放(或者兩者都是)的同步點溶锭。
在調(diào)用這個函數(shù)之前發(fā)生的釋放線程的所有可見的副作用都是同步的,在調(diào)用這個函數(shù)在獲取線程之前符隙。
調(diào)用該函數(shù)與加載或存儲原子操作具有相同的效果趴捅,但不涉及原子值。
-
atomic_signal_fence
“C”風(fēng)格導(dǎo)出的函數(shù):
extern "C" void atomic_signal_fence (memory_order sync) noexcept;
建立一個單線程的隔離:對這個函數(shù)的調(diào)用點在一個線程內(nèi)變成一個獲取或一個釋放點(或者兩者)霹疫。
這個函數(shù)相當于atomic_thread_fence 拱绑,但是沒有因為調(diào)用而發(fā)生線程間同步。該函數(shù)的作用是對編譯器進行指令丽蝎,以阻止它進行優(yōu)化猎拨,包括將寫操作移動到一個釋放柵欄或在獲取柵欄之前的讀操作。
五屠阻、 Functions for atomic objects (C-style)
template (1)
template <class T> bool atomic_is_lock_free (const volatile atomic<T>* obj) noexcept;
template <class T> bool atomic_is_lock_free (const atomic<T>* obj) noexcept;
overloads (2)
bool atomic_is_lock_free (const volatile A* obj) noexcept;
bool atomic_is_lock_free (const A* obj) noexcept;
是否是lock_free宿崭。
注意:
除了std::atomicflag的所有原子類型都可以使用互斥鎖或其他鎖定操作實現(xiàn)穿香,而不是使用無鎖的原子CPU指令垃喊。原子類型也被允許有時是無鎖的又憨,例如,如果在給定的體系結(jié)構(gòu)中麻诀,只有對齊的內(nèi)存訪問是自然的痕寓,那么相同類型的不一致的對象就必須使用鎖。
C++標準推薦(但不需要)無鎖的原子操作也是無地址的蝇闭,也就是說呻率,適合于使用共享內(nèi)存的進程之間的通信。
示例15 (同示例3):
#include <iostream>
#include <utility>
#include <atomic>
struct A { int a[100]; };
struct B { int x, y; };
int main()
{
std::atomic<A> a;
std::atomic<B> b;
std::cout << std::boolalpha
<< "std::atomic<A> is lock free? "
<< std::atomic_is_lock_free(&a) << '\n'
<< "std::atomic<B> is lock free? "
<< std::atomic_is_lock_free(&b) << '\n';
return 0;
}
template (1)
template <class T> void atomic_init (volatile atomic<T>* obj, T val) noexcept;
template <class T> void atomic_init (atomic<T>* obj, T val) noexcept;
overloads (2)
void atomic_init (volatile A* obj, T val) noexcept;
void atomic_init (A* obj, T val) noexcept;
用val的一個包含值初始化obj,該函數(shù)不是原子性的:來自另一個線程的并發(fā)訪問丁眼,即使是通過原子操作筷凤,也是一種數(shù)據(jù)競爭。
如果obj不是默認構(gòu)造,那么這個行為就沒有定義藐守。
如果這個函數(shù)在同一個obj上被調(diào)用兩次挪丢,那么這個行為是沒有定義的。
template (1)
template <class T> void atomic_store (volatile atomic<T>* obj, T val) noexcept;
template <class T> void atomic_store (atomic<T>* obj, T val) noexcept;
template <class T>
void atomic_store_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept;
template <class T>
void atomic_store_explicit (atomic<T>* obj, T val, memory_order sync) noexcept;
overloads (2)
void atomic_store (volatile A* obj, T val) noexcept;
void atomic_store (A* obj, T val) noexcept;
void atomic_store_explicit (volatile A* obj, T val, memory_order sync) noexcept;
void atomic_store_explicit (A* obj, T val, memory_order sync) noexcept;
用val替換obj中包含的值卢厂。該操作是原子性的乾蓬,atomic_store 使用順序一致性(memory_order_seq_cst), atomic_store_explicit 顯示制定內(nèi)存順序。參見stomic的等價函數(shù)atomic::store和 atomic::operator=慎恒。
template (1)
template <class T> T atomic_load (const volatile atomic<T>* obj) noexcept;
template <class T> T atomic_load (const atomic<T>* obj) noexcept;
template <class T>
T atomic_load_explicit (const volatile atomic<T>* obj, memory_order sync) noexcept;
template <class T>
T atomic_load_explicit (const atomic<T>* obj, memory_order sync) noexcept;
overloads (2)
T atomic_load (const volatile A* obj) noexcept;
T atomic_load (const A* obj) noexcept;
T atomic_load_explicit (const volatile A* obj, memory_order sync) noexcept;
T atomic_load_explicit (const A* obj, memory_order sync) noexcept;
同上面函數(shù)任内,返回obj中包含的值。該操作是原子性的融柬,并使用順序一致性(memory_order_seq_cst)死嗦。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_load_explicit。
template (1)
template <class T> T atomic_exchange (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_exchange (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_exchange_explicit(volatile atomic<T>* obj, T val, memory_order sync) noexcept;
template <class T>
T atomic_exchange_explicit(atomic<T>* obj, T val, memory_order sync) noexcept;
overloads (2)
T atomic_exchange (volatile A* obj, T val) noexcept;
T atomic_exchange (A* obj, T val) noexcept;
T atomic_exchange_explicit(volatile A* obj, T val, memory_order sync) noexcept;
T atomic_exchange_explicit(A* obj, T val, memory_order sync) noexcept;
用val替換obj中包含的值粒氧,并返回obj之前的值越除。
整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的瞬間之間,obj的值不會受到其他線程的影響外盯。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_exchange_explicit摘盆。
注意: 這個顯示代碼網(wǎng)站上有錯誤,本文已經(jīng)改正饱苟。
template (1)
template <class T>
bool atomic_compare_exchange_weak (volatile atomic<T>* obj, T* expected, T val) noexcept;
template <class T>
bool atomic_compare_exchange_weak (atomic<T>* obj, T* expected, T val) noexcept;
template <class T>
bool atomic_compare_exchange_weak_explicit (volatile atomic<T>* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
template <class T>
bool atomic_compare_exchange_weak_explicit (atomic<T>* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
overloads (2)
bool atomic_compare_exchange_weak (volatile A* obj, T* expected, T val) noexcept;
bool atomic_compare_exchange_weak (A* obj, T* expected, T val) noexcept;
bool atomic_compare_exchange_weak_explicit (volatile A* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
bool atomic_compare_exchange_weak_explicit (A* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
將obj中包含的值與預(yù)期的內(nèi)容進行比較:
-如果是真的孩擂,它會用val替換包含的值。
-如果是假的箱熬,它將用所包含的值替換預(yù)期值
函數(shù)總是訪問包含的值來讀取它类垦,如果這個比較是真的,那么它也會替換它坦弟。但是整個操作都是原子性的:在讀取值和被替換的時刻之間护锤,它的值不能被其他線程修改。
** 注意:
這個函數(shù)直接的比較物理內(nèi)容所包含的價值與預(yù)期的內(nèi)容,這可能導(dǎo)致得到使用operator==比較的結(jié)果是一個失敗的結(jié)果,因為對象底層的物理內(nèi)容中可能存在位對齊或其他邏輯表示相同但是物理表示不同的值(比如 true 和 2 或 3酿傍,它們在邏輯上都表示"真",但在物理上兩者的表示并不相同)驱入。
不像 compare_exchange_strong赤炒,這個弱版本允許(spuriously 地)返回 false(即原子對象所封裝的值與參數(shù) expected 的物理內(nèi)容相同,但卻仍然返回 false)亏较,即使在預(yù)期的實際情況與所包含的對象相比較時也是如此莺褒。對于某些循環(huán)算法來說,這可能是可接受的行為雪情,并且可能會在某些平臺上帶來顯著的性能提升遵岩。在這些虛假的失敗中,函數(shù)返回false,而不修改預(yù)期尘执。
對于非循環(huán)算法來說舍哄, compare_exchange_strong通常是首選。
template (1)
template <class T>
bool atomic_compare_exchange_strong (volatile atomic<T>* obj, T* expected, T val) noexcept;
template <class T>
bool atomic_compare_exchange_strong (atomic<T>* obj, T* expected, T val) noexcept;
template <class T>
bool atomic_compare_exchange_strong_explicit (volatile atomic<T>* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
template <class T>
bool atomic_compare_exchange_strong_explicit (atomic<T>* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
overloads (2)
bool atomic_compare_exchange_strong (volatile A* obj, T* expected, T val) noexcept;
bool atomic_compare_exchange_strong (A* obj, T* expected, T val) noexcept;
bool atomic_compare_exchange_strong_explicit (volatile A* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
bool atomic_compare_exchange_strong_explicit (A* obj,
T* expected, T val, memory_order success, memory_order failure) noexcept;
將obj中包含的值與預(yù)期的值進行比較:
-如果是真的誊锭,它會用val替換包含的值表悬。
-如果是假的,它將用所包含的值替換預(yù)期值丧靡。
函數(shù)總是訪問包含的值來讀取它蟆沫,如果這個比較是真的,那么它也會替換它温治。但是整個操作都是原子性的:在讀取值和被替換的時刻之間饭庞,它的值不能被其他線程修改。
** 注意:
compare_exchange_strong 跟 compare_exchange_week 不同的是:
與compare_exchange_weak 不同, strong版本的 compare-and-exchange 操作不允許(spuriously 地)返回 false熬荆,即原子對象所封裝的值與參數(shù) expected 的物理內(nèi)容相同但绕,比較操作一定會為 true。不過在某些平臺下惶看,如果算法本身需要循環(huán)操作來做檢查捏顺, compare_exchange_weak 的性能會更好。
template (integral) (1)
template <class T> T atomic_fetch_add (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_fetch_add (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_fetch_add_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept;
template <class T>
T atomic_fetch_add_explicit (atomic<T>* obj, T val, memory_order sync) noexcept;
template (pointer) (2)
template <class U> U* atomic_fetch_add (volatile atomic<U*>* obj, ptrdiff_t val) noexcept;
template <class U> U* atomic_fetch_add (atomic<U*>* obj, ptrdiff_t val) noexcept;
template <class U>
U* atomic_fetch_add_explicit (volatile atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept;
template <class U>
U* atomic_fetch_add_explicit (atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept;
overloads (3)
T atomic_fetch_add (volatile A* obj, M val) noexcept;
T atomic_fetch_add (A* obj, M val) noexcept;
T atomic_fetch_add_explicit (volatile A* obj, M val, memory_order sync) noexcept;
T atomic_fetch_add_explicit (A* obj, M val, memory_order sync) noexcept;
將val添加到obj中所包含的值纬黎。整個操作都是原子性的:在讀取(返回)值和被該函數(shù)修改的時刻之間幅骄,值不能被修改。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_fetch_add_explicit 本今。
template (integral) (1)
template <class T> T atomic_fetch_sub (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_fetch_sub (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_fetch_sub_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept;
template <class T>
T atomic_fetch_sub_explicit (atomic<T>* obj, T val, memory_order sync) noexcept;
template (pointer) (2)
template <class U> U* atomic_fetch_sub (volatile atomic<U*>* obj, ptrdiff_t val) noexcept;
template <class U> U* atomic_fetch_sub (atomic<U*>* obj, ptrdiff_t val) noexcept;
template <class U>
U* atomic_fetch_sub_explicit (volatile atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept;
template <class U>
U* atomic_fetch_sub_explicit (atomic<U*>* obj, ptrdiff_t val, memory_order sync) noexcept;
overloads (3)
T atomic_fetch_sub (volatile A* obj, M val) noexcept;
T atomic_fetch_sub (A* obj, M val) noexcept;
T atomic_fetch_sub_explicit (volatile A* obj, M val, memory_order sync) noexcept;
T atomic_fetch_sub_explicit (A* obj, M val, memory_order sync) noexcept;
從obj中所包含的值減去val拆座。整個操作都是原子性的:在讀取(返回)值和被該函數(shù)修改的時刻之間,值不能被修改冠息。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_fetch_sub_explicit 挪凑。
template (integral) (1)
template <class T> T atomic_fetch_and (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_fetch_and (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_fetch_and_explicit (volatile atomic<T>* obj,T val, memory_order sync) noexcept;
template <class T>
T atomic_fetch_and_explicit (atomic<T>* obj,T val, memory_order sync) noexcept;
overloads (2)
T atomic_fetch_and (volatile A* obj, T val) noexcept;
T atomic_fetch_and (A* obj, T val) noexcept;
T atomic_fetch_and_explicit (volatile A* obj, T val, memory_order sync) noexcept;
T atomic_fetch_and_explicit (A* obj, T val, memory_order sync) noexcept;
讀取obj中包含的值,并通過在讀取值和val之間執(zhí)行一個位操作“與”操作來替換它逛艰。
整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的那一刻之間躏碳,值不會受到其他線程的影響。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_fetch_and_explicit 散怖。
template (integral) (1)
template <class T> T atomic_fetch_or (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_fetch_or (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_fetch_or_explicit (volatile atomic<T>* obj, T val, memory_order sync) noexcept;
template <class T>
T atomic_fetch_or_explicit (atomic<T>* obj,T val, memory_order sync) noexcept;
overloads (2)
T atomic_fetch_or (volatile A* obj, T val) noexcept;
T atomic_fetch_or (A* obj, T val) noexcept;
T atomic_fetch_or_explicit (volatile A* obj, T val, memory_order sync) noexcept;
T atomic_fetch_or_explicit (A* obj, T val, memory_order sync) noexcept;
讀取obj中包含的值菇绵,并通過在讀取值和val之間執(zhí)行一個位操作“或”操作來替換它。
整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的那一刻之間镇眷,值不會受到其他線程的影響咬最。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_fetch_or_explicit
template (integral) (1)
template <class T> T atomic_fetch_xor (volatile atomic<T>* obj, T val) noexcept;
template <class T> T atomic_fetch_xor (atomic<T>* obj, T val) noexcept;
template <class T>
T atomic_fetch_xor_explicit (volatile atomic<T>* obj,T val, memory_order sync) noexcept;
template <class T>
T atomic_fetch_xor_explicit (atomic<T>* obj,T val, memory_order sync) noexcept;
overloads (2)
T atomic_fetch_xor (volatile A* obj, T val) noexcept;
T atomic_fetch_xor (A* obj, T val) noexcept;
T atomic_fetch_xor_explicit (volatile A* obj, T val, memory_order sync) noexcept;
T atomic_fetch_xor_explicit (A* obj, T val, memory_order sync) noexcept;
讀取obj中包含的值,并通過在讀取值和val之間執(zhí)行一個位操作“異或”操作來替換它欠动。
整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的那一刻之間永乌,值不會受到其他線程的影響。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_fetch_xor_explicit
bool atomic_flag_test_and_set (volatile atomic_flag* obj) noexcept;
bool atomic_flag_test_and_set (atomic_flag* obj) noexcept;
bool atomic_flag_test_and_set (volatile atomic_flag* obj, memory_order sync) noexcept;
bool atomic_flag_test_and_set (atomic_flag* obj, memory_order sync) noexcept;
設(shè)置obj指向的原子標志,并返回調(diào)用之前的值翅雏。
整個操作是原子性的(一個原子的讀-修改-寫操作):在讀取(返回)值和被該函數(shù)修改的瞬間之間圈驼,obj的值不會受到其他線程的影響。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_flag_test_and_set
void atomic_flag_clear (volatile atomic_flag* obj) noexcept;
void atomic_flag_clear (atomic_flag* obj) noexcept;
void atomic_flag_clear_explicit(volatile atomic_flag* obj, memory_order sync) noexcept;
void atomic_flag_clear_explicit(atomic_flag* obj, memory_order sync) noexcept;
清除obj枚荣,將它的標志值設(shè)置為false碗脊。該操作是原子性的。要制定內(nèi)存數(shù)序使用顯示調(diào)用函數(shù)atomic_flag_clear_explicit橄妆。
六衙伶、Macro functions
- ATOMIC_VAR_INIT
ATOMIC_VAR_INIT(val) //初始化 std::atomic 對象。
這個宏的存在是為了與C實現(xiàn)兼容害碾,在其中矢劲,它被用作構(gòu)造函數(shù)(默認構(gòu)造的)原子對象;在C++中,這個初始化可以由初始化構(gòu)造函數(shù)直接執(zhí)行慌随。 - ATOMIC_FLAG_INIT
ATOMIC_FLAG_INIT //初始化 std::atomic_flag 對象芬沉。
這個宏的定義用來將類型std::atomic_flag的對象初始化到clear的狀態(tài)。
本文主要介紹atomic阁猜,下一篇介紹 c++11 多線程(4) condition_varible 總結(jié)丸逸。