C++11 線程同步接口std::condition_variable和std::future的簡(jiǎn)單使用示例詳解
std::condition_variable
條件變量std::condition_variable有wait和notify接口用于線程間的同步。如下圖所示,Thread 2阻塞在wait接口,Thread 1通過(guò)notify接口通知Thread 2繼續(xù)執(zhí)行。
具體參見(jiàn)示例代碼:
#include<iostream> #include<mutex> #include<thread> #include<queue> std::mutex mt; std::queue<int> data; std::condition_variable cv; auto start=std::chrono::high_resolution_clock::now(); void logCurrentTime() { auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); std::cout << elapsed << ":"; } void prepare_data() { logCurrentTime(); std::cout << "this is " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl; for (int i = 0; i < 10; i++) { data.push(i); logCurrentTime(); std::cout << "data OK:" << i << std::endl; } //start to notify consume_data thread data is OK! cv.notify_one(); } void consume_data() { logCurrentTime(); std::cout << "this is: " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl; std::unique_lock<std::mutex> lk(mt); //wait first for notification cv.wait(lk); //it must accept a unique_lock parameter to wait while (!data.empty()) { logCurrentTime(); std::cout << "data consumed: " << data.front() << std::endl; data.pop(); } } int main() { std::thread t2(consume_data); //wait for a while to wait first then prepare data,otherwise stuck on wait std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::thread t1(prepare_data); t1.join(); t2.join(); return 0; }
分析
主線程中另啟兩個(gè)線程,分別執(zhí)行consume_data和prepare_data,其中consume_data要先執(zhí)行,以保證先等待再通知,否則若先通知再等待就死鎖了。首先consume_data線程在從wait 處阻塞等待。后prepare_data線程中依次向隊(duì)列寫入0-10,寫完之后通過(guò)notify_one 通知consume_data線程解除阻塞,依次讀取0-10。
std::future
std::future與std::async配合異步執(zhí)行代碼,再通過(guò)wait或get接口阻塞當(dāng)前線程等待結(jié)果。如下圖所示,Thread 2中future接口的get或wait接口會(huì)阻塞當(dāng)前線程,std::async異步開(kāi)啟的新線程Thread1執(zhí)行結(jié)束后,將結(jié)果存于std::future后通知Thread 1獲取結(jié)果后繼續(xù)執(zhí)行.
具體參見(jiàn)如下代碼:
#include <iostream> #include <future> #include<thread> int test() { std::cout << "this is " << __FUNCTION__ << " thread:" << std::this_thread::get_id() << std::endl;; std::this_thread::sleep_for(std::chrono::microseconds(1000)); return 10; } int main() { std::cout << "this is " <<__FUNCTION__<<" thread:" << std::this_thread::get_id() << std::endl;; //this will lanuch on another thread std::future<int> result = std::async(test); std::cout << "After lanuch a thread: "<< std::this_thread::get_id() << std::endl; //block the thread and wait for the result std::cout << "result is: " <<result.get()<< std::endl; std::cout << "After get result "<< std::endl; return 0; }
輸出結(jié)果
分析主程序中調(diào)用std::async異步調(diào)用test函數(shù),可以看到main函數(shù)的線程ID 27428與test函數(shù)執(zhí)行的線程ID 9704并不一樣,說(shuō)明std::async另起了一個(gè)新的線程。在test線程中,先sleep 1000ms,所以可以看到"After lanuch a thread:"先輸出,說(shuō)明主線程異步執(zhí)行,不受子線程影響。而"After get result "最后輸出,說(shuō)明get()方法會(huì)阻塞主線程,直到獲取結(jié)果。
作者:robot2017
出處:https://www.cnblogs.com/stephen2023/p/18416810
版權(quán):本文版權(quán)歸作者和博客園共有
轉(zhuǎn)載:歡迎轉(zhuǎn)載,但未經(jīng)作者同意,必須保留此段聲明;必須在文章中給出原文連接;否則必究法律責(zé)任
到此這篇關(guān)于C++11 線程同步接口std::condition_variable和std::future的簡(jiǎn)單使用示例詳解的文章就介紹到這了,更多相關(guān)C++11 線程同步接口std::condition_variable和std::future內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
實(shí)例解析C++設(shè)計(jì)模式編程中簡(jiǎn)單工廠模式的采用
這篇文章主要介紹了C++設(shè)計(jì)模式編程中簡(jiǎn)單工廠模式的采用實(shí)例,在簡(jiǎn)單工廠模式中程序往往利用封裝繼承來(lái)降低耦合度,需要的朋友可以參考下2016-03-03使用kendynet構(gòu)建異步redis訪問(wèn)服務(wù)
這篇文章主要介紹了在kendynet上寫的一個(gè)簡(jiǎn)單的redis異步訪問(wèn)接口,大家參考使用吧2014-01-01C++之a(chǎn)ssert推薦用法及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于C++之a(chǎn)ssert推薦用法及注意事項(xiàng)的相關(guān)資料,assert 是一個(gè)預(yù)處理宏,用于在運(yùn)行時(shí)檢查表達(dá)式是否為真,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07