最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++11 atomic的使用詳解

 更新時(shí)間:2021年02月26日 16:43:18   作者:后端技術(shù)小屋  
這篇文章主要介紹了c++11 atomic的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下

std::atomic_flag

  std::atomic_flag是一個(gè)原子的布爾類型,可支持兩種原子操作:

  • test_and_set, 如果atomic_flag對象被設(shè)置,則返回true; 如果atomic_flag對象未被設(shè)置,則設(shè)置之,返回false
  • clear. 清楚atomic_flag對象

  std::atomic_flag可用于多線程之間的同步操作,類似于linux中的信號量。使用atomic_flag可實(shí)現(xiàn)mutex.

#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <sstream>

std::atomic_flag lock = ATOMIC_FLAG_INIT;
std::stringstream stream;

void append_numer(int x)
{
 while (lock.test_and_set());
 stream << "thread#" << x << "\n";
 lock.clear();
}

int main()
{
 std::vector<std::thread> ths;
 for (int i=0; i<10; i++)
  ths.push_back(std::thread(append_numer, i));
 for (int i=0; i<10; i++)
  ths[i].join();
 std::cout << stream.str();
 return 0;
}

std::atomic

  std::atomic對int, char, bool等數(shù)據(jù)結(jié)構(gòu)進(jìn)行原子性封裝,在多線程環(huán)境中,對std::atomic對象的訪問不會造成競爭-冒險(xiǎn)。利用std::atomic可實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的無鎖設(shè)計(jì)。

#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <sstream>

std::atomic<bool> ready(false);
std::atomic_flag winner = ATOMIC_FLAG_INIT;

void count1m(int i)
{
 while (!ready);
 for (int i=0; i<1000000; i++);
 if (!winner.test_and_set())
  std::cout << "winner: " << i << std::endl;
}

int main()
{
 std::vector<std::thread> ths;
 for (int i=0; i<10; i++)
  ths.push_back(std::thread(count1m, i));
 ready = true;
 for (int i=0; i<10; i++)
  ths[i].join();
 return 0;
}

  在上例中,執(zhí)行read=true之后,所有線程結(jié)束空等。winner被初始化為ATOMIC_FLAG_INIT,最先執(zhí)行winner.test_and_set并返回false的線程為winner。

#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <sstream>

std::atomic<int> foo(0);

void set_foo(int x)
{
 foo = x;
}

void print_foo()
{
 while (foo == 0)
 {
  std::this_thread::yield();
 }
 std::cout << "x: " << foo << std::endl;
}
int main()
{
 std::thread print_th(print_foo);
 std::thread set_th(set_foo, 10);
 print_th.join();
 set_th.join();
 return 0;
}

  在上例總,set_foo用于設(shè)置atomic<int>對象的值,print_foo用于打印atomic<int>對象的值。std::atomic對象的值的讀取和寫入可使用load和store實(shí)現(xiàn)。

#include <iostream>
#include <cassert>
#include <atomic>
#include <vector>
#include <unistd.h>
#include <thread>
#include <sstream>

std::atomic<int> foo(0);
std::atomic_flag lock = ATOMIC_FLAG_INIT;

void add_foo()
{
 while (1) 
 {
  foo++;
  // foo = foo + 1;
  while (lock.test_and_set());
  std::cout <<"add: " << foo << std::endl;
  lock.clear();
  usleep(1000);
 }
}

void sub_foo()
{
 while (1) 
 {
  foo--;
  // foo = foo - 1;
  while (lock.test_and_set());
  std::cout << "sub: " << foo << std::endl;
  lock.clear();
  usleep(1000);
 }
}
int main()
{
 std::thread th2 = std::thread(add_foo);
 std::thread th1 = std::thread(sub_foo);
 th1.join();
 th2.join();
 return 0;
}

  atomic<int>支持++和--的原子操作。

以上就是c++11 atomic的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于c++11 atomic的使用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))

    C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++ 中try finally關(guān)鍵字詳解

    C++ 中try finally關(guān)鍵字詳解

    本文給大家介紹C++ 中try finally關(guān)鍵字的相關(guān)知識,非常不錯(cuò),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • C++協(xié)程實(shí)現(xiàn)序列生成器的案例分享

    C++協(xié)程實(shí)現(xiàn)序列生成器的案例分享

    序列生成器通常的實(shí)現(xiàn)是在一個(gè)協(xié)程內(nèi)部通過某種方式向外部傳一個(gè)值出去,并且將自己掛起,本文圍繞序列生成器這個(gè)經(jīng)典的協(xié)程案例介紹了協(xié)程的銷毀、co_await 運(yùn)算符、await_transform 以及 yield_value 的用法,需要的朋友可以參考下
    2024-05-05
  • VC++ 6.0 C語言實(shí)現(xiàn)俄羅斯方塊詳細(xì)教程

    VC++ 6.0 C語言實(shí)現(xiàn)俄羅斯方塊詳細(xì)教程

    這篇文章主要為大家介紹了VC++ 6.0 C語言實(shí)現(xiàn)俄羅斯方塊詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 基于C++ Lambda表達(dá)式的程序優(yōu)化

    基于C++ Lambda表達(dá)式的程序優(yōu)化

    這篇文章主要介紹了基于C++ Lambda表達(dá)式的程序優(yōu)化的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • C++中std::construct()與std::destroy()的使用

    C++中std::construct()與std::destroy()的使用

    std::construct()和std::destroy()是C++ STL中的函數(shù)模板,用于在已分配的存儲區(qū)域中構(gòu)造或銷毀對象,本文主要介紹了C++中std::construct()與std::destroy()的使用,感興趣的可以了解一下
    2024-02-02
  • C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù)

    C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù)

    這篇文章主要為大家詳細(xì)介紹了C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • C語言實(shí)現(xiàn)支持動(dòng)態(tài)拓展和銷毀的線程池

    C語言實(shí)現(xiàn)支持動(dòng)態(tài)拓展和銷毀的線程池

    這篇文章主要為大家介紹了C語言實(shí)現(xiàn)支持動(dòng)態(tài)拓展和銷毀的線程池,感興趣的小伙伴們可以參考一下
    2016-01-01
  • QT中QStringListModel類的應(yīng)用介紹

    QT中QStringListModel類的應(yīng)用介紹

    QStringListModel是最簡單的模型類,具備向視圖提供字符串?dāng)?shù)據(jù)的能力,本文主要介紹了QT中QStringListModel類的應(yīng)用介紹,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • c++文件監(jiān)控之FileSystemWatcher

    c++文件監(jiān)控之FileSystemWatcher

    為了監(jiān)控web程序的靜態(tài)文件是否被惡意改動(dòng),所以學(xué)習(xí)了一下FileSystemWatcher 類對文件的監(jiān)控,由于還在初級階段,這里只貼一下關(guān)于FileSystemWatcher學(xué)習(xí)的一些代碼
    2019-04-04

最新評論

什邡市| 清远市| 丹棱县| 南京市| 宁强县| 太仓市| 柳河县| 太仆寺旗| 清新县| 毕节市| 开远市| 屏东县| 宁津县| 安龙县| 淄博市| 临海市| 舒城县| 昔阳县| 马龙县| 山东省| 建宁县| 威宁| 嘉鱼县| 汉沽区| 泰宁县| 双桥区| 阿尔山市| 五华县| 图木舒克市| 伊金霍洛旗| 乌拉特中旗| 溧阳市| 三门峡市| 秦安县| 蒲城县| 开平市| 普定县| 平塘县| 江达县| 克拉玛依市| 郴州市|