C++在多線程中使用condition_variable實現(xiàn)wait
前言
有這樣的需求
一個線程需要等待另一個線程執(zhí)行完畢之后它才會繼續(xù)向下執(zhí)行,這該如何實現(xiàn)? condition_variable類中實現(xiàn)了wait方法
wait()
- 可以接受兩個參數(shù),其中第一個參數(shù)是鎖對象,第二個參數(shù)是lambda表達式,其中的lambda表達式的返回值要是bool類型
- 也可接受一個參數(shù),僅僅接受鎖對象
#include<iostream> #include<mutex> #include<list> #include<thread> using namespace std; class Obj { private: list<int> myList; condition_variable var; mutex tex; public: bool popFromList(int & comb) { if (!myList.empty()) { unique_lock<mutex> cur(tex); if (!myList.empty()) { comb = myList.front(); myList.pop_front(); return true; } } return false; } void inToList() { for (int i = 0; i < 100; i++) { unique_lock<mutex> cur(tex); myList.push_back(i); cout << this_thread::get_id() << "正在向list中添加數(shù)據(jù)>; " << i << endl; var.notify_one(); } } void outFromList() { while(true) { unique_lock<mutex> cur(tex); var.wait(cur, [this] { if (!myList.empty()) return true; return false; }); int comb = myList.front(); myList.pop_front(); cout << this_thread::get_id() << "正在取出數(shù)據(jù)>: " << comb << endl; } } }; int main() { Obj obj; thread thread1(&Obj::outFromList, &obj); thread thread2(&Obj::inToList, &obj); thread1.join(); thread2.join(); cout << "這里是主線程" << endl; }
線程1執(zhí)行到wait函數(shù)時,會執(zhí)行l(wèi)ambda表達式
返回值為false時,這個線程就會將鎖打開,將該線程壓入到棧中,執(zhí)行下一個線程 下一個線程執(zhí)行完畢之后執(zhí)行notify_one后就會返回到該線程,繼續(xù)執(zhí)行l(wèi)ambda表達式
返回值為true時就嘗試獲得鎖
獲得鎖后繼續(xù)執(zhí)行下面的語句
沒有獲得鎖就繼續(xù)卡在wait等待獲取到鎖
返回值為false時,繼續(xù)將鎖打開,線程壓入棧,執(zhí)行下一個線程
返回值為true時,線程繼續(xù)執(zhí)行
運行結果可能是一個線程執(zhí)行完畢另一個線程再執(zhí)行
這不就是順序執(zhí)行嗎?
其實并不是這樣的
notify_one()
喚醒wait()函數(shù),當前所再的線程嘗試獲得鎖
某個線程執(zhí)行完notify_one函數(shù)后,會返回到另一個線程中的wait函數(shù)處,并將其"喚醒",讓其繼續(xù)執(zhí)行,自己的線程和wait線程都嘗試獲得鎖來進行下一步執(zhí)行
不是順序執(zhí)行的解釋
- 當wait函數(shù)后面有很多語句要執(zhí)行,但是再此之前wait所在的線程函數(shù)中就已經(jīng)將鎖進行釋放了,那么notify_one的“喚醒”就不在生效,兩個線程都嘗試獲得鎖,但是wait所在的線程有很多語句要執(zhí)行,耗時高,那么很有可能notify_one所在的線程就再次獲得了鎖,進行下一步操作。
- 這樣就不一定是順序執(zhí)行的宏觀表現(xiàn)了
#include<iostream> #include<mutex> #include<list> #include<thread> using namespace std; class Obj { private: list<int> myList; condition_variable var; mutex tex; public: bool popFromList(int & comb) { if (!myList.empty()) { unique_lock<mutex> cur(tex); if (!myList.empty()) { comb = myList.front(); myList.pop_front(); return true; } } return false; } void inToList() { for (int i = 0; i < 1000; i++) { cout << this_thread::get_id() << "正在向list中添加數(shù)據(jù)>; " << i << endl; unique_lock<mutex> cur(tex); myList.push_back(i); var.notify_one(); } } void outFromList() { while(true) { unique_lock<mutex> cur(tex); var.wait(cur, [this] { if (!myList.empty()) return true; return false; }); int comb = myList.front(); myList.pop_front(); cur.unlock(); cout << this_thread::get_id() << "正在取出數(shù)據(jù)>: " << comb << endl; chrono::seconds tim(2); this_thread::sleep_for(tim); cout <<this_thread::get_id() << "睡眠兩秒后執(zhí)行" << endl; } } }; int main() { Obj obj; thread thread1(&Obj::outFromList, &obj); thread thread2(&Obj::inToList, &obj); thread1.join(); thread2.join(); cout << "這里是主線程" << endl; }
如圖所示,在notify_one線程執(zhí)行835次循環(huán)后,wait所在的線程才獲得了鎖,繼續(xù)執(zhí)行
在此之前的所有notify_one的喚醒操作都是無效的。
然后在notify_one線程執(zhí)行完畢之后,wait再次獲得了鎖,繼續(xù)向下執(zhí)行
notify_all()
顧名思義,是“喚醒”所有的wait函數(shù)線程,但是并不是都一起運行,因為他們要進行鎖的請求,僅僅只能由一把鎖存在,只有獲得鎖并且lambda表達式返回結果為true的線程才能繼續(xù)執(zhí)行
到此這篇關于C++在多線程中使用condition_variable實現(xiàn)wait的文章就介紹到這了,更多相關C++ condition_variable內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言數(shù)據(jù)結構與算法時間空間復雜度基礎實踐
這篇文章主要為大家介紹了C語言數(shù)據(jù)結構與算法中時間空間復雜度的基礎實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02