生活随笔
收集整理的這篇文章主要介紹了
C++ boost thread学习(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 條件變量?
如果線程之間執行順序上有依賴關系,可使用條件變量(Condition variables)。?
可以到boost官網中參考條件變量(Condition variables)的使用。?
條件變量必須和互斥量配合使用,等待另一個線程重某個事件的發生(滿足某個條件),然后線程才能繼續執行。
共有兩種條件變量對象condition_variable, condition_variable_any,一般情況下使用condition_variable_any。?
條件變量的使用方式:?
擁有條件變量的線程先鎖定互斥量,然后循環檢查某個條件,如果條件不滿足,那么就調用條件變量的成員函數wait()等待直到條件滿足。其他線程處理條件變量要求的條件,當條件滿足時調用它的成員函數notify_one()或者notify_all(),以通知一個或者所有正在等待條件的變量的線程停止等待繼續執行。?
例子:生產--消費模型。?
緩沖區buffer使用了兩個條件變量cond_put和cond_get,分別用于處理put動作和get動作,如果緩沖區滿則cond_put持續等待,當cond_put得到通知 (緩沖區不滿)時線程寫入數據,然后通知cond_get條件變量可以獲取數據。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;? ????int?un_read,?capacity;?? ????bool?is_full()? ????{?? ????????return?un_read?==?capacity;?? ????}?? ????bool?is_empty()?? ????{?? ????????return?un_read?==?0;?? ????}?? ?? public:?? ????buffer(size_t?n)?:?un_read(0),?capacity(n){}?? ????void?put(int?x)?? ????{?? ????????{? ????????????boost::mutex::scoped_lock?lock(mu);? ????????????while?(?is_full()?)? ????????????{?? ????????????????{? ????????????????????boost::mutex::scoped_lock?lock(io_mu);?? ????????????????????cout?<<?"full?waiting..."?<<?endl;?? ????????????????}?? ????????????????cond_put.wait(mu);? ????????????}? ????????????stk.push(x);? ????????????++un_read;?? ????????}? ????????cond_get.notify_one();? ????}?? ?? ????void?get(int?*x)? ????{?? ????????{? ????????????boost::mutex::scoped_lock?lock(mu);? ????????????while?(is_empty())? ????????????{?? ????????????????{?? ????????????????????boost::mutex::scoped_lock?lock(io_mu);?? ????????????????????cout?<<?"empty?waiting..."?<<?endl;?? ????????????????}?? ????????????????cond_get.wait(mu);? ????????????}?? ????????????--un_read;?? ????????????*x?=?stk.top();? ????????????stk.pop();? ????????}?? ????????cond_put.notify_one();? ????}?? };?? ?? buffer?buf(5);? void?producter(int?n)? {?? ????for?(int?i?=?0;?i?<?n;?++i)?? ????{?? ????????{?? ????????????boost::mutex::scoped_lock?lock(io_mu);?? ????????????cout?<<?"put?"?<<?i?<<?endl;?? ????????}?? ????????buf.put(i);? ????}?? }?? ?? void?consumer(int?n)? {?? ????int?x;?? ????for?(int?i?=?0;?i?<?n;?++i)?? ????{?? ????????buf.get(&x);? ????????boost::mutex::scoped_lock?lock(io_mu);?? ????????cout?<<?"get?"?<<?x?<<?endl;?? ????}?? }?? ?? int?main()?? {?? ????boost::thread?t1(producter,?20);? ????boost::thread?t2(consumer,?10);? ????boost::thread?t3(consumer,?10);?? ?? ????t1.join();?? ????t2.join();?? ????t3.join();?? ?? ????return?0;?? }?? 運行結果:?
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?
轉載于:https://www.cnblogs.com/fire909090/p/6801543.html
總結
以上是生活随笔 為你收集整理的C++ boost thread学习(二) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。