日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++11 并发指南六( atomic 类型详解二 std::atomic )

發布時間:2025/3/15 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11 并发指南六( atomic 类型详解二 std::atomic ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++11 并發指南六(atomic 類型詳解一 atomic_flag 介紹)? 一文介紹了 C++11 中最簡單的原子類型 std::atomic_flag,但是 std::atomic_flag 過于簡單,只提供了 test_and_set 和 clear 兩個 API,不能滿足其他需求(如 store, load, exchange, compare_exchange 等),因此本文將介紹功能更加完善的 std::atomic 類。

std::atomic 基本介紹

std::atomic 是模板類,一個模板類型為 T 的原子對象中封裝了一個類型為 T 的值。

template <class T> struct atomic;

原子類型對象的主要特點就是從不同線程訪問不會導致數據競爭(data race)。因此從不同線程訪問某個原子對象是良性 (well-defined) 行為,而通常對于非原子類型而言,并發訪問某個對象(如果不做任何同步操作)會導致未定義 (undifined) 行為發生。

C++11 標準中的基本 std::atomic 模板定義如下:

template < class T > struct atomic {bool is_lock_free() const volatile;bool is_lock_free() const;void store(T, memory_order = memory_order_seq_cst) volatile;void store(T, memory_order = memory_order_seq_cst);T load(memory_order = memory_order_seq_cst) const volatile;T load(memory_order = memory_order_seq_cst) const;operator T() const volatile;operator T() const;T exchange(T, memory_order = memory_order_seq_cst) volatile;T exchange(T, memory_order = memory_order_seq_cst);bool compare_exchange_weak(T &, T, memory_order, memory_order) volatile;bool compare_exchange_weak(T &, T, memory_order, memory_order);bool compare_exchange_strong(T &, T, memory_order, memory_order) volatile;bool compare_exchange_strong(T &, T, memory_order, memory_order);bool compare_exchange_weak(T &, T, memory_order = memory_order_seq_cst) volatile;bool compare_exchange_weak(T &, T, memory_order = memory_order_seq_cst);bool compare_exchange_strong(T &, T, memory_order = memory_order_seq_cst) volatile;bool compare_exchange_strong(T &, T, memory_order = memory_order_seq_cst);atomic() = default;constexpr atomic(T);atomic(const atomic &) = delete;atomic & operator=(const atomic &) = delete;atomic & operator=(const atomic &) volatile = delete;T operator=(T) volatile;T operator=(T); };

另外,C++11 標準庫 std::atomic 提供了針對整形(integral)和指針類型的特化實現,分別定義如下:

針對整形(integal)的特化,其中 integal 代表了如下類型char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t:

?
template<> struct atomic<integral> { ????boolis_lock_free() constvolatile; ????boolis_lock_free() const; ????voidstore(integral, memory_order = memory_order_seq_cst) volatile; ????voidstore(integral, memory_order = memory_order_seq_cst); ????integral load(memory_order = memory_order_seq_cst)const volatile; ????integral load(memory_order = memory_order_seq_cst)const; ????operator integral()const volatile; ????operator integral()const; ????integral exchange(integral, memory_order = memory_order_seq_cst)volatile; ????integral exchange(integral, memory_order = memory_order_seq_cst); ????boolcompare_exchange_weak(integral&, integral, memory_order, memory_order)volatile; ????boolcompare_exchange_weak(integral&, integral, memory_order, memory_order); ????boolcompare_exchange_strong(integral&, integral, memory_order, memory_order)volatile; ????boolcompare_exchange_strong(integral&, integral, memory_order, memory_order); ????boolcompare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst)volatile; ????boolcompare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst); ????boolcompare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst)volatile; ????boolcompare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst); ????integral fetch_add(integral, memory_order = memory_order_seq_cst)volatile; ????integral fetch_add(integral, memory_order = memory_order_seq_cst); ????integral fetch_sub(integral, memory_order = memory_order_seq_cst)volatile; ????integral fetch_sub(integral, memory_order = memory_order_seq_cst); ????integral fetch_and(integral, memory_order = memory_order_seq_cst)volatile; ????integral fetch_and(integral, memory_order = memory_order_seq_cst); ????integral fetch_or(integral, memory_order = memory_order_seq_cst)volatile; ????integral fetch_or(integral, memory_order = memory_order_seq_cst); ????integral fetch_xor(integral, memory_order = memory_order_seq_cst)volatile; ????integral fetch_xor(integral, memory_order = memory_order_seq_cst); ????? ????atomic() =default; ????constexpratomic(integral); ????atomic(constatomic&) = delete; ????atomic& operator=(constatomic&) = delete; ????atomic& operator=(constatomic&) volatile= delete; ????? ????integral operator=(integral)volatile; ????integral operator=(integral); ????? ????integral operator++(int)volatile; ????integral operator++(int); ????integral operator--(int)volatile; ????integral operator--(int); ????integral operator++()volatile; ????integral operator++(); ????integral operator--()volatile; ????integral operator--(); ????integral operator+=(integral)volatile; ????integral operator+=(integral); ????integral operator-=(integral)volatile; ????integral operator-=(integral); ????integral operator&=(integral)volatile; ????integral operator&=(integral); ????integral operator|=(integral)volatile; ????integral operator|=(integral); ????integral operator^=(integral)volatile; ????integral operator^=(integral); };

針對指針的特化:

?
template<class T> struct atomic<T*> { ????boolis_lock_free() constvolatile; ????boolis_lock_free() const; ????voidstore(T*, memory_order = memory_order_seq_cst) volatile; ????voidstore(T*, memory_order = memory_order_seq_cst); ????T* load(memory_order = memory_order_seq_cst)const volatile; ????T* load(memory_order = memory_order_seq_cst)const; ????operator T*()const volatile; ????operator T*()const; ????T* exchange(T*, memory_order = memory_order_seq_cst)volatile; ????T* exchange(T*, memory_order = memory_order_seq_cst); ????boolcompare_exchange_weak(T*&, T*, memory_order, memory_order)volatile; ????boolcompare_exchange_weak(T*&, T*, memory_order, memory_order); ????boolcompare_exchange_strong(T*&, T*, memory_order, memory_order)volatile; ????boolcompare_exchange_strong(T*&, T*, memory_order, memory_order); ????boolcompare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst)volatile; ????boolcompare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst); ????boolcompare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst)volatile; ????boolcompare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst); ????T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst)volatile; ????T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst); ????T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst)volatile; ????T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst); ????atomic() =default; ????constexpratomic(T*); ????atomic(constatomic&) = delete; ????atomic& operator=(constatomic&) = delete; ????atomic& operator=(constatomic&) volatile= delete; ????T* operator=(T*)volatile; ????T* operator=(T*); ????T* operator++(int)volatile; ????T* operator++(int); ????T* operator--(int)volatile; ????T* operator--(int); ????T* operator++()volatile; ????T* operator++(); ????T* operator--()volatile; ????T* operator--(); ????T* operator+=(ptrdiff_t)volatile; ????T* operator+=(ptrdiff_t); ????T* operator-=(ptrdiff_t)volatile; ????T* operator-=(ptrdiff_t); };

std::atomic 成員函數

?好了,對 std::atomic 有了一個最基本認識之后我們來看 std::atomic 的成員函數吧。

std::atomic 構造函數

std::atomic 的構造函數如下:

default (1)initialization (2)copy [deleted] (3)
atomic() noexcept = default;
constexpr atomic (T val) noexcept;
atomic (const atomic&) = delete;
  • 默認構造函數,由默認構造函數創建的 std::atomic 對象處于未初始化(uninitialized)狀態,對處于未初始化(uninitialized)狀態 std::atomic對象可以由 atomic_init 函數進行初始化。
  • 初始化構造函數,由類型 T初始化一個 std::atomic對象。
  • 拷貝構造函數被禁用。
  • 請看下例:

    ?
    #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 // 由 false 初始化一個 std::atomic<bool> 類型的原子變量 std::atomic<bool> ready(false); std::atomic_flag winner = ATOMIC_FLAG_INIT; voiddo_count1m(intid) { ????while(!ready) { std::this_thread::yield(); } // 等待 ready 變為 true. ????for(volatile int i=0; i<1000000; ++i) {} // 計數 ????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(); ????return0; }

    std::atomic::operator=() 函數

    std::atomic 的賦值操作函數定義如下:

    set value (1)copy [deleted] (2)
    T operator= (T val) noexcept; T operator= (T val) volatile noexcept;
    atomic& operator= (const atomic&) = delete; atomic& operator= (const atomic&) volatile = delete;

    可以看出,普通的賦值拷貝操作已經被禁用。但是一個類型為 T 的變量可以賦值給相應的原子類型變量(相當與隱式轉換),該操作是原子的,內存序(Memory Order) 默認為順序一致性(std::memory_order_seq_cst),如果需要指定其他的內存序,需使用 std::atomic::store()。

    ?
    #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;// 調用 std::atomic::operator=(). } voidprint_foo() { ????while(foo == 0) { // wait while foo == 0 ????????std::this_thread::yield(); ????} ????std::cout <<"foo: " << foo << '\n'; } int main() { ????std::threadfirst(print_foo); ????std::threadsecond(set_foo, 10); ????first.join(); ????second.join(); ????return0; }

    基本 std::atomic 類型操作

    本節主要介紹基本 std::atomic 類型所具備的操作(即成員函數)。我們知道 std::atomic 是模板類,一個模板類型為 T 的原子對象中封裝了一個類型為 T 的值。本文<std::atomic 基本介紹>一節中也提到了 std::atomic 類模板除了基本類型以外,還針對整形和指針類型做了特化。 特化的 std::atomic 類型支持更多的操作,如 fetch_add, fetch_sub, fetch_and 等。本小節介紹基本 std::atomic 類型所具備的操作:

    is_lock_free ?
    bool is_lock_free() const volatile noexcept; bool is_lock_free() const noexcept;
    判斷該 std::atomic 對象是否具備 lock-free 的特性。如果某個對象滿足 lock-free 特性,在多個線程訪問該對象時不會導致線程阻塞。(可能使用某種事務內存transactional memory方法實現 lock-free 的特性)。 store ?
    void store (T val, memory_order sync = memory_order_seq_cst) volatilenoexcept; void store (T val, memory_order sync = memory_order_seq_cst) noexcept;
    修改被封裝的值,std::atomic::store 函數將類型為 T 的參數 val 復制給原子對象所封裝的值。T 是 std::atomic 類模板參數。另外參數 sync 指定內存序(Memory Order),可能的取值如下:
    Memory Order 值Memory Order 類型
    memory_order_relaxedRelaxed
    memory_order_releaseRelease
    memory_order_seq_cstSequentially consistent
    請看下面例子: ?
    #include <iostream>?????? // std::cout #include <atomic>???????? // std::atomic, std::memory_order_relaxed #include <thread>???????? // std::thread std::atomic<int> foo(0);// 全局的原子對象 foo void set_foo(int x) { ????foo.store(x, std::memory_order_relaxed);// 設置(store) 原子對象 foo 的值 } voidprint_foo() { ????intx; ????do{ ????????x = foo.load(std::memory_order_relaxed);// 讀取(load) 原子對象 foo 的值 ????}while (x == 0); ????std::cout <<"foo: " << x << '\n'; } int main () { ????std::threadfirst(print_foo); // 線程 first 打印 foo 的值 ????std::threadsecond(set_foo, 10); // 線程 second 設置 foo 的值 ????first.join(); ????second.join(); ????return0; }
    load ?
    T load (memory_order sync = memory_order_seq_cst)const volatile noexcept; T load (memory_order sync = memory_order_seq_cst)const noexcept;
    讀取被封裝的值,參數 sync 設置內存序(Memory Order),可能的取值如下: Memory Order 值Memory Order 類型
    memory_order_relaxedRelaxed
    memory_order_consumeConsume
    memory_order_acquireAcquire
    memory_order_seq_cstSequentially consistent
    請看下面例子: ?
    #include <iostream>?????? // std::cout #include <atomic>???????? // std::atomic, std::memory_order_relaxed #include <thread>???????? // std::thread std::atomic<int> foo(0);// 全局的原子對象 foo void set_foo(int x) { ????foo.store(x, std::memory_order_relaxed);// 設置(store) 原子對象 foo 的值 } voidprint_foo() { ????intx; ????do{ ????????x = foo.load(std::memory_order_relaxed);// 讀取(load) 原子對象 foo 的值 ????}while (x == 0); ????std::cout <<"foo: " << x << '\n'; } int main () { ????std::threadfirst(print_foo); // 線程 first 打印 foo 的值 ????std::threadsecond(set_foo, 10); // 線程 second 設置 foo 的值 ????first.join(); ????second.join(); ????return0; }
    operator T ?
    operator T() const volatilenoexcept; operator T() const noexcept;
    與 load 功能類似,也是讀取被封裝的值,operator T() 是類型轉換(type-cast)操作,默認的內存序是 std::memory_order_seq_cst,如果需要指定其他的內存序,你應該使用 load() 函數。請看下面例子: ?
    #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; } voidcopy_foo_to_bar() { ????// 如果 foo == 0,則該線程 yield, ????// 在 foo == 0 時, 實際也是隱含了類型轉換操作, ????// 因此也包含了 operator T() const 的調用. ????while(foo == 0) std::this_thread::yield(); ????// 實際調用了 operator T() const, 將foo 強制轉換成 int 類型, ????// 然后調用 operator=(). ????bar =static_cast<int>(foo); } voidprint_bar() { ????// 如果 bar == 0,則該線程 yield, ????// 在 bar == 0 時, 實際也是隱含了類型轉換操作, ????// 因此也包含了 operator T() const 的調用. ????while(bar == 0) std::this_thread::yield(); ????std::cout <<"bar: " << bar << '\n'; } int main () { ????std::threadfirst(print_bar); ????std::threadsecond(set_foo, 10); ????std::threadthird(copy_foo_to_bar); ????first.join(); ????second.join(); ????third.join(); ????return0; }

    ?

    exchange 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; 讀取并修改被封裝的值,exchange 會將 val 指定的值替換掉之前該原子對象封裝的值,并返回之前該原子對象封裝的值,整個過程是原子的(因此exchange 操作也稱為read-modify-write 操作)。sync參數指定內存序(Memory Order),可能的取值如下: Memory Order 值Memory Order 類型
    memory_order_relaxedRelaxed
    memory_order_consumeConsume
    memory_order_acquireAcquire
    memory_order_releaseRelease
    memory_order_acq_relAcquire/Release
    memory_order_seq_cstSequentially consistent

    請看下面例子,各個線程計數至 1M,首先完成計數任務的線程打印自己的 ID,

    ?
    #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(); ????return0; }
    compare_exchange_weak (1)(2)
    bool compare_exchange_weak (T& expected, T val,memory_order sync = memory_order_seq_cst) volatile noexcept; bool compare_exchange_weak (T& expected, T val,memory_order sync = memory_order_seq_cst) noexcept;
    bool compare_exchange_weak (T& expected, T val,memory_order success, memory_order failure) volatile noexcept; bool compare_exchange_weak (T& expected, T val,memory_order success, memory_order failure) noexcept;
    比較并交換被封裝的值(weak)與參數?expected 所指定的值是否相等,如果:
    • 相等,則用 val 替換原子對象的舊值。
    • 不相等,則用原子對象的舊值替換 expected ,因此調用該函數之后,如果被該原子對象封裝的值與參數?expected 所指定的值不相等,expected 中的內容就是原子對象的舊值。
    該函數通常會讀取原子對象封裝的值,如果比較為 true(即原子對象的值等于 expected),則替換原子對象的舊值,但整個操作是原子的,在某個線程讀取和修改該原子對象時,另外的線程不能對讀取和修改該原子對象。

    在第(2)種情況下,內存序(Memory Order)的選擇取決于比較操作結果,如果比較結果為 true(即原子對象的值等于 expected),則選擇參數 success 指定的內存序,否則選擇參數 failure 所指定的內存序。
    注意,該函數直接比較原子對象所封裝的值與參數 expected 的物理內容,所以某些情況下,對象的比較操作在使用 operator==() 判斷時相等,但 compare_exchange_weak 判斷時卻可能失敗,因為對象底層的物理內容中可能存在位對齊或其他邏輯表示相同但是物理表示不同的值(比如 true 和 2 或 3,它們在邏輯上都表示"真",但在物理上兩者的表示并不相同)。
    與compare_exchange_strong 不同, weak 版本的 compare-and-exchange 操作允許(spuriously 地)返回 false(即原子對象所封裝的值與參數expected 的物理內容相同,但卻仍然返回 false),不過在某些需要循環操作的算法下這是可以接受的,并且在一些平臺下?compare_exchange_weak 的性能更好 。如果 compare_exchange_weak 的判斷確實發生了偽失敗(spurious failures)——即使原子對象所封裝的值與參數expected 的物理內容相同,但判斷操作的結果卻為 false,compare_exchange_weak函數返回 false,并且參數 expected 的值不會改變。
    對于某些不需要采用循環操作的算法而言, 通常采用compare_exchange_strong 更好。另外,該函數的內存序由? sync 參數指定,可選條件如下: Memory Order 值Memory Order 類型
    memory_order_relaxedRelaxed
    memory_order_consumeConsume
    memory_order_acquireAcquire
    memory_order_releaseRelease
    memory_order_acq_relAcquire/Release
    memory_order_seq_cstSequentially consistent
    請看下面的例子(參考): ?
    #include <iostream>?????? // std::cout #include <atomic>???????? // std::atomic #include <thread>???????? // std::thread #include <vector>???????? // std::vector // a simple global linked list: structNode { int value; Node* next; }; std::atomic<Node*> list_head(nullptr); voidappend(int val) { ????// append an element to the list ????Node* newNode =new Node{val, list_head}; ????// next is the same as: list_head = newNode, but in a thread-safe way: ????while(!list_head.compare_exchange_weak(newNode->next,newNode)) {} ????// (with newNode->next updated accordingly if some other thread just appended another node) } 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;} ????return0; }
    可能的執行結果如下: ?
    9 8 7 6 5 4 3 2 1 0

    ?

    compare_exchange_strong (1)(2)
    bool compare_exchange_strong (T& expected, T val,memory_order sync = memory_order_seq_cst) volatile noexcept; bool compare_exchange_strong (T& expected, T val,memory_order sync = memory_order_seq_cst) noexcept;
    bool compare_exchange_strong (T& expected, T val,memory_order success, memory_order failure) volatile noexcept; bool compare_exchange_strong (T& expected, T val,memory_order success, memory_order failure) noexcept;
    比較并交換被封裝的值(strong)與參數?expected 所指定的值是否相等,如果:
    • 相等,則用 val 替換原子對象的舊值。
    • 不相等,則用原子對象的舊值替換 expected ,因此調用該函數之后,如果被該原子對象封裝的值與參數?expected 所指定的值不相等,expected 中的內容就是原子對象的舊值。
    該函數通常會讀取原子對象封裝的值,如果比較為 true(即原子對象的值等于 expected),則替換原子對象的舊值,但整個操作是原子的,在某個線程讀取和修改該原子對象時,另外的線程不能對讀取和修改該原子對象。

    在第(2)種情況下,內存序(Memory Order)的選擇取決于比較操作結果,如果比較結果為 true(即原子對象的值等于 expected),則選擇參數 success 指定的內存序,否則選擇參數 failure 所指定的內存序。
    注意,該函數直接比較原子對象所封裝的值與參數 expected 的物理內容,所以某些情況下,對象的比較操作在使用 operator==() 判斷時相等,但 compare_exchange_weak 判斷時卻可能失敗,因為對象底層的物理內容中可能存在位對齊或其他邏輯表示相同但是物理表示不同的值(比如 true 和 2 或 3,它們在邏輯上都表示"真",但在物理上兩者的表示并不相同)。 與compare_exchange_weak 不同, strong版本的 compare-and-exchange 操作不允許(spuriously 地)返回 false,即原子對象所封裝的值與參數expected 的物理內容相同,比較操作一定會為 true。不過在某些平臺下,如果算法本身需要循環操作來做檢查, compare_exchange_weak 的性能會更好。
    因此對于某些不需要采用循環操作的算法而言, 通常采用compare_exchange_strong 更好。另外,該函數的內存序由? sync 參數指定,可選條件如下: Memory Order 值Memory Order 類型
    memory_order_relaxedRelaxed
    memory_order_consumeConsume
    memory_order_acquireAcquire
    memory_order_releaseRelease
    memory_order_acq_relAcquire/Release
    memory_order_seq_cstSequentially consistent
    請看下面的例子: ?
    #include <iostream>?????? // std::cout #include <atomic>???????? // std::atomic #include <thread>???????? // std::thread #include <vector>???????? // std::vector // a simple global linked list: structNode { int value; Node* next; }; std::atomic<Node*> list_head(nullptr); voidappend(int val) { ????// append an element to the list ????Node* newNode =new Node{val, list_head}; ????// next is the same as: list_head = newNode, but in a thread-safe way: ????while(!(list_head.compare_exchange_strong(newNode->next, newNode))); ????// (with newNode->next updated accordingly if some other thread just appended another node) } 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;} ????return0; }


    好了,本文花了大量的篇幅介紹 std::atomic 基本類型,下一篇博客我會給大家介紹 C++11 的標準庫中std::atomic 針對整形(integral)和指針類型的特化版本做了哪些改進。

    總結

    以上是生活随笔為你收集整理的C++11 并发指南六( atomic 类型详解二 std::atomic )的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 粗大黑人巨茎大战欧美成人免费看 | 午夜久久 | 亚洲中文一区二区三区 | 久久精工是国产品牌吗 | 男人深夜网站 | 成人av在线播放网站 | 国产女人在线视频 | 蜜桃视频久久一区免费观看入口 | 亚州av综合色区无码一区 | 一区二区在线看 | 国产网站视频 | 久久国产一级 | 俄罗斯破处 | 日本一区二区三区在线看 | 国产又粗又猛又爽又黄的网站 | av手机免费观看 | 天天干妹子 | 国产羞羞 | 91黄色免费视频 | √天堂资源在线 | 伦理片一区二区 | 污片在线免费观看 | 乡村乱淫 | 99精品欧美一区二区三区 | 欧美videossex极品 | 水密桃av | 日本美女啪啪 | 亚洲性视频 | 中国吞精videos露脸 | www.久久艹| 中文字幕乱码免费 | 欧美视频一区二区三区 | 夜夜骑狠狠干 | 成年人av在线播放 | 伊人春色网 | 一区二区三区在线免费观看视频 | 欧美人在线| 国产一级免费av | 国产亚洲视频在线 | 天天操天天操天天操 | 精品一区二区在线看 | 亚洲精品自拍视频 | 国产亚洲av在线 | 中国久久久 | 国产精品三区四区 | 在线免费观看污视频 | 欧美久久影院 | 天天干天天噜 | 亚洲精品成人电影 | 欧洲女性下面有没有毛发 | 美女aaa| 99爱国产 | 久久久亚洲av波多野结衣 | 91精品一区二区三 | 激情图片网站 | 天干夜天干天天天爽视频 | 日韩一级在线 | 国模一区二区三区 | 天天性综合 | 欧美日韩精品一区二区 | 叶爱在线 | 精品少妇人妻一区二区黑料社区 | 欧美操操操| 91av手机在线 | 黄网免费观看 | 不卡av电影在线 | 色欲av无码精品一区 | 亚洲免费在线 | 九色porny自拍视频在线播放 | 五月天青青草 | 天天综合欧美 | 日韩一级中文字幕 | 全黄一级片 | 国产激情福利 | 色婷婷精品 | 日本在线免费观看 | 高清在线一区二区 | 17c在线视频| 欧美性猛交富婆 | 欧美a v在线 | 精品午夜一区二区 | 日本一级淫片色费放 | 中文字幕在线观看播放 | 亚洲精品中文字幕乱码无线 | 国产精品久久久久久久久久 | 高跟肉丝丝袜呻吟啪啪网站av | 在线免费h | 老司机深夜视频 | 97se视频| 美国黄色一级大片 | 欧美在线一区二区三区 | 欧美成在线视频 | 日b免费视频 | 亚洲在线观看一区 | 日韩欧美大片 | 十大污网站 | 日韩电影一区二区 | 琪琪伦伦影院理论片 | 女人舌吻男人茎视频 |