C++11?condition_variable條件變量的用法說明
1 什么是條件變量
condition_variable是一個類,常和mutex搭配使用。
condition_variable類是一個同步原語,可用于阻塞一個線程或同時阻止多個線程,直到另一個線程修改共享變量并通知condition_variable。
防止多線程場景下,共享變量混亂。
理解條件變量要先理解三個概念:
- 鎖 (鎖住共享變量,線程獨(dú)占)
wait
等待 (等待通知條件變量,變化的共享變量是否滿足條件)notify
通知 (通知等待的條件變量,共享變量發(fā)送變化)
2 condition_variable類定義
2.1 wait函數(shù)
void wait( std::unique_lockstd::mutex& lock ); //Predicate是lambda表達(dá)式。 template< class Predicate > void wait( std::unique_lockstd::mutex& lock, Predicate pred ); //以上二者都被notify_one())或notify_broadcast()喚醒,但是 //第二種方式是喚醒后也要滿足Predicate的條件。 //如果不滿足條件,繼續(xù)解鎖互斥量,然后讓線程處于阻塞或等待狀態(tài)。 //第二種等價(jià)于 while (!pred()) { wait(lock); }
3 condition_variable用法
condition_variable必定至少有兩方,一方是資源修改線程,一方是資源等待線程。就跟打籃球一樣,同時籃球只會在一個人手中,投籃后就釋放了籃球所有權(quán),其他方就會搶奪籃球所有權(quán)。
3.1 資源修改線程步驟
- 獲取一個mutex使用 std::unique_lock< std::mutex >
- 保持鎖定狀態(tài),修改共享變量
- condition_variable對象執(zhí)行notify_one或者notify_all(notify_one/notify_all執(zhí)行前可以釋放鎖)
3.2 資源等待線程步驟
- 獲取一個mutex使用 std::unique_lock< std::mutex > unlock用于保護(hù)要修改的共享變量
- 檢查條件變量,
(1)條件變量滿足,線程繼續(xù)執(zhí)行
(2)條件變量不滿足,wait會釋放unlock鎖,并掛起線程。
- 當(dāng)notify通知條件變量、超時過期或發(fā)生虛假喚醒時,線程被喚醒,互斥鎖unlock被原子地重新獲取。然后,線程應(yīng)該檢查條件,如果喚醒是假的,則繼續(xù)等待
4 代碼示例
4.1 無需notify場景
當(dāng)wait第一次執(zhí)行是,條件已經(jīng)滿足,則程序不會阻塞(即無需notify),會直接向下執(zhí)行。(僅為說明3.2 中第2點(diǎn)(1)的情況)
#include <iostream> #include <string> #include <thread> #include <mutex> #include <condition_variable> using namespace std; std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void worker_thread() { std::cout << "3、worker_thread子線程開始執(zhí)行" << endl; // Wait until main() sends data std::unique_lock<std::mutex> lk(m); std::cout << "4、worker_thread子線程獲取到鎖,條件滿足無需notify,不阻塞向下執(zhí)行" << endl; cv.wait(lk, []{return ready;}); // after the wait, we own the lock. data += " after processing"; // Send data back to main() processed = true; std::cout << "5、Worker thread signals data processing completed\n"; // Manual unlocking is done before notifying, to avoid waking up // the waiting thread only to block again (see notify_one for details) lk.unlock(); std::cout << "6、worker_thread子線程交出執(zhí)行權(quán)限,主線程執(zhí)行" << endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); cv.notify_one(); std::cout << "9、worker_thread調(diào)用 notify_one" << endl; } int main() { std::thread worker(worker_thread); std::cout << "1、主線程開始執(zhí)行" << std::endl; data = "Example data"; // send data to the worker thread { //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::lock_guard<std::mutex> lk(m); ready = true; } std::cout << "2、鎖已經(jīng)釋放了,主線程休眠,子線程執(zhí)行" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); //cv.notify_one(); { std::cout << "7、主線程data:" << data << endl; std::unique_lock<std::mutex> lk(m); std::cout << "8、主線程條件滿足無需notify" << endl; cv.wait(lk, []{return processed;}); } worker.join(); std::cout << "10、主線程結(jié)束" << endl; }
執(zhí)行結(jié)果:
4.2 正常應(yīng)用場景1
#include <iostream> #include <string> #include <thread> #include <mutex> #include <condition_variable> using namespace std; std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void worker_thread() { std::cout << "3、worker_thread子線程開始執(zhí)行" << endl; // Wait until main() sends data std::unique_lock<std::mutex> lk(m); std::cout << "4、worker_thread子線程獲取到鎖,條件不滿足,釋放lk鎖,子線程阻塞" << endl; cv.wait(lk, []{return ready;}); std::cout << "8、worker_thread子線程獲取到鎖,子線程繼續(xù)執(zhí)行" << endl; // after the wait, we own the lock. data += " after processing"; // Send data back to main() processed = true; std::cout << "9、Worker thread signals data processing completed\n"; // Manual unlocking is done before notifying, to avoid waking up // the waiting thread only to block again (see notify_one for details) lk.unlock(); std::this_thread::sleep_for(std::chrono::milliseconds(5000)); std::cout << "10、worker_thread調(diào)用 notify_one通知主線程執(zhí)行" << endl; cv.notify_one(); } int main() { std::thread worker(worker_thread); std::cout << "1、主線程開始執(zhí)行" << std::endl; data = "Example data"; // send data to the worker thread { std::cout << "2、主線程休眠,子線程進(jìn)入執(zhí)行" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::cout << "5、主線程結(jié)束休眠,主線程獲取lk鎖,進(jìn)入執(zhí)行" << std::endl; std::lock_guard<std::mutex> lk(m); ready = true; } std::cout << "6、主線程釋放lk,調(diào)用notify通知子線程" << std::endl; cv.notify_one(); { std::cout << "7、由于主線程的執(zhí)行時鐘周期未結(jié)束,繼續(xù)執(zhí)行主線程獲取lk, wait檢查條件不滿足,釋放鎖" << endl; std::unique_lock<std::mutex> lk(m); cv.wait(lk, []{return processed;}); } worker.join(); std::cout << "11、主線程結(jié)束" << endl; }
執(zhí)行結(jié)果:
這里notify執(zhí)行后不一定立即執(zhí)行子線程,如果cpu執(zhí)行時鐘周期未結(jié)束,則主線程會繼續(xù)執(zhí)行. 所以7,8,9,10順序可能變化參見4.3
同時4.1也會因?yàn)閏pu時鐘周期,執(zhí)行順序有所變動。
4.3 正常應(yīng)用場景2
#include <iostream> #include <string> #include <thread> #include <mutex> #include <condition_variable> using namespace std; std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void worker_thread() { std::cout << "3、worker_thread子線程開始執(zhí)行" << endl; // Wait until main() sends data std::unique_lock<std::mutex> lk(m); std::cout << "4、worker_thread子線程獲取到鎖,條件不滿足,釋放lk鎖,子線程阻塞" << endl; cv.wait(lk, []{return ready;}); std::cout << "8、worker_thread子線程獲取到鎖,子線程繼續(xù)執(zhí)行" << endl; // after the wait, we own the lock. data += " after processing"; // Send data back to main() processed = true; std::cout << "9、Worker thread signals data processing completed\n"; // Manual unlocking is done before notifying, to avoid waking up // the waiting thread only to block again (see notify_one for details) lk.unlock(); std::cout << "10、worker_thread調(diào)用 notify_one通知主線程執(zhí)行" << endl; cv.notify_one(); } int main() { std::thread worker(worker_thread); std::cout << "1、主線程開始執(zhí)行" << std::endl; data = "Example data"; // send data to the worker thread { std::cout << "2、主線程休眠,子線程進(jìn)入執(zhí)行" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::cout << "5、主線程結(jié)束休眠,主線程獲取lk鎖,進(jìn)入執(zhí)行" << std::endl; std::lock_guard<std::mutex> lk(m); ready = true; } std::cout << "6、主線程釋放lk,調(diào)用notify通知子線程" << std::endl; cv.notify_one(); { for(int i = 0; i< 10000000; i++) { int j = i; } std::cout << "7、由于主線程的執(zhí)行時鐘周期未結(jié)束,繼續(xù)執(zhí)行主線程獲取lk, wait檢查條件不滿足,釋放鎖" << endl; std::unique_lock<std::mutex> lk(m); cv.wait(lk, []{return processed;}); } worker.join(); std::cout << "11、主線程結(jié)束" << endl; }
執(zhí)行結(jié)果:
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語言基于graphics.h實(shí)現(xiàn)圣誕樹
這篇文章主要介紹了圣誕樹代碼,c語言編程,基于graphics.h實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12C++實(shí)現(xiàn)Dijkstra算法的示例代碼
迪杰斯特拉算法(Dijkstra)是由荷蘭計(jì)算機(jī)科學(xué)家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是從一個頂點(diǎn)到其余各頂點(diǎn)的最短路徑算法。本文將用C++實(shí)現(xiàn)Dijkstra算法,需要的可以參考一下2022-07-07C++實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(Map實(shí)現(xiàn))
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06c++和python實(shí)現(xiàn)順序查找實(shí)例
這篇文章主要介紹了c++和python實(shí)現(xiàn)順序查找實(shí)例,流程即將目標(biāo)數(shù)值和數(shù)據(jù)庫中的每個數(shù)值進(jìn)行比較,如果相同則搜索完成,如果不同則繼續(xù)比較下一處,下面來看看具體的實(shí)例操作吧,需要的朋友可以參考一下2022-03-03