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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ boost thread学习(二)

發(fā)布時間:2025/7/14 c/c++ 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ boost thread学习(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

條件變量?

如果線程之間執(zhí)行順序上有依賴關(guān)系,可使用條件變量(Condition variables)。?

可以到boost官網(wǎng)中參考條件變量(Condition variables)的使用。?

條件變量必須和互斥量配合使用,等待另一個線程重某個事件的發(fā)生(滿足某個條件),然后線程才能繼續(xù)執(zhí)行。

共有兩種條件變量對象condition_variable, condition_variable_any,一般情況下使用condition_variable_any。?

條件變量的使用方式:?
擁有條件變量的線程先鎖定互斥量,然后循環(huán)檢查某個條件,如果條件不滿足,那么就調(diào)用條件變量的成員函數(shù)wait()等待直到條件滿足。其他線程處理?xiàng)l件變量要求的條件,當(dāng)條件滿足時調(diào)用它的成員函數(shù)notify_one()或者notify_all(),以通知一個或者所有正在等待條件的變量的線程停止等待繼續(xù)執(zhí)行。?

例子:生產(chǎn)--消費(fèi)模型。?

緩沖區(qū)buffer使用了兩個條件變量cond_put和cond_get,分別用于處理put動作和get動作,如果緩沖區(qū)滿則cond_put持續(xù)等待,當(dāng)cond_put得到通知 (緩沖區(qū)不滿)時線程寫入數(shù)據(jù),然后通知cond_get條件變量可以獲取數(shù)據(jù)。cond_get的處理流程與cond_put類似。?

C++代碼??
  • #include?<boost/thread.hpp>??
  • #include?<boost/thread/mutex.hpp>??
  • ??
  • #include?<iostream>??
  • #include?<stack>??
  • ??
  • using?namespace?std;??
  • ??
  • boost::mutex?io_mu;??
  • ??
  • class?buffer??
  • {??
  • private:??
  • ????boost::mutex?mu;?//?互斥量,配合條件變量使用??
  • ????boost::condition_variable_any?cond_put;?//?寫入條件變量??
  • ????boost::condition_variable_any?cond_get;?//?讀取條件變量??
  • ??
  • ????stack<int>?stk;?//?緩沖區(qū)對象??
  • ????int?un_read,?capacity;??
  • ????bool?is_full()?//?緩沖區(qū)滿判斷??
  • ????{??
  • ????????return?un_read?==?capacity;??
  • ????}??
  • ????bool?is_empty()??//?緩沖區(qū)空判斷??
  • ????{??
  • ????????return?un_read?==?0;??
  • ????}??
  • ??
  • public:??
  • ????buffer(size_t?n)?:?un_read(0),?capacity(n){}??//?構(gòu)造函數(shù)??
  • ????void?put(int?x)??//?寫入數(shù)據(jù)??
  • ????{??
  • ????????{?//?開始一個局部域??
  • ????????????boost::mutex::scoped_lock?lock(mu);?//鎖定互斥量??
  • ????????????while?(?is_full()?)?//?檢查緩沖區(qū)是否滿??
  • ????????????{??
  • ????????????????{?//?局部域,鎖定cout輸出一條信息??
  • ????????????????????boost::mutex::scoped_lock?lock(io_mu);??
  • ????????????????????cout?<<?"full?waiting..."?<<?endl;??
  • ????????????????}??
  • ????????????????cond_put.wait(mu);?//?條件變量等待??
  • ????????????}?//?條件變臉滿足,停止等待??
  • ????????????stk.push(x);?//?壓棧,寫入數(shù)據(jù)??
  • ????????????++un_read;??
  • ????????}?//?解鎖互斥量,條件變量的通知不需要互斥量鎖定??
  • ????????cond_get.notify_one();?//?通知可以讀取數(shù)據(jù)??
  • ????}??
  • ??
  • ????void?get(int?*x)?//?讀取數(shù)據(jù)??
  • ????{??
  • ????????{?//?局部域開始??
  • ????????????boost::mutex::scoped_lock?lock(mu);?//?鎖定互斥量??
  • ????????????while?(is_empty())?//?檢查緩沖區(qū)是否空??
  • ????????????{??
  • ????????????????{??
  • ????????????????????boost::mutex::scoped_lock?lock(io_mu);??
  • ????????????????????cout?<<?"empty?waiting..."?<<?endl;??
  • ????????????????}??
  • ????????????????cond_get.wait(mu);?//?條件變量等待??
  • ????????????}??
  • ????????????--un_read;??
  • ????????????*x?=?stk.top();?//?讀取數(shù)據(jù)??
  • ????????????stk.pop();?//?彈棧??
  • ????????}??
  • ????????cond_put.notify_one();?//?通知可以寫入數(shù)據(jù)??
  • ????}??
  • };??
  • ??
  • buffer?buf(5);?//?一個緩沖區(qū)對象??
  • void?producter(int?n)?//?生產(chǎn)者??
  • {??
  • ????for?(int?i?=?0;?i?<?n;?++i)??
  • ????{??
  • ????????{??
  • ????????????boost::mutex::scoped_lock?lock(io_mu);??
  • ????????????cout?<<?"put?"?<<?i?<<?endl;??
  • ????????}??
  • ????????buf.put(i);?//?寫入數(shù)據(jù)??
  • ????}??
  • }??
  • ??
  • void?consumer(int?n)?//?消費(fèi)者??
  • {??
  • ????int?x;??
  • ????for?(int?i?=?0;?i?<?n;?++i)??
  • ????{??
  • ????????buf.get(&x);?//?讀取數(shù)據(jù)??
  • ????????boost::mutex::scoped_lock?lock(io_mu);??
  • ????????cout?<<?"get?"?<<?x?<<?endl;??
  • ????}??
  • }??
  • ??
  • int?main()??
  • {??
  • ????boost::thread?t1(producter,?20);?//?一個生產(chǎn)者線程??
  • ????boost::thread?t2(consumer,?10);?//?兩個消費(fèi)者線程??
  • ????boost::thread?t3(consumer,?10);??
  • ??
  • ????t1.join();??
  • ????t2.join();??
  • ????t3.join();??
  • ??
  • ????return?0;??
  • }??


  • 運(yùn)行結(jié)果:?
    empty waiting...?
    put 0?
    empty waiting...?
    put 1?
    put 2?
    get 1?
    get 2?
    get 0?
    empty waiting...?
    empty waiting...?
    put 3?
    put 4?
    put 5?
    put 6?
    put 7?
    get 6?
    get 7?
    get 5?
    get 4?
    get 3?
    empty waiting...?
    put 8?
    empty waiting...?
    put 9?
    put 10?
    put 11?
    get 9?
    get 11?
    get 8?
    empty waiting...?
    put 12?
    put 13?
    put 14?
    put 15?
    put 16?
    put 17?
    full waiting...?
    get 10?
    get 16?
    put 18?
    full waiting...?
    get 17?
    get 15?
    get 14?
    get 13?
    get 12?
    get 18?
    empty waiting...?
    put 19?
    get 19?

    轉(zhuǎn)載于:https://www.cnblogs.com/fire909090/p/6801543.html

    總結(jié)

    以上是生活随笔為你收集整理的C++ boost thread学习(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。