C++11 線程同步接口std::condition_variable和std::future的簡單使用
std::condition_variable
條件變量std::condition_variable有wait和notify接口用于線程間的同步。如下圖所示,Thread 2阻塞在wait接口,Thread 1通過notify接口通知Thread 2繼續執行。
具體參見示例代碼:
#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;
}
輸出結果

分析
主線程中另啟兩個線程,分別執行consume_data和prepare_data,其中consume_data要先執行,以保證先等待再通知,否則若先通知再等待就死鎖了。首先consume_data線程在從wait 處阻塞等待。后prepare_data線程中依次向隊列寫入0-10,寫完之后通過notify_one 通知consume_data線程解除阻塞,依次讀取0-10。
std::future
std::future與std::async配合異步執行代碼,再通過wait或get接口阻塞當前線程等待結果。如下圖所示,Thread 2中future接口的get或wait接口會阻塞當前線程,std::async異步開啟的新線程Thread1執行結束后,將結果存于std::future后通知Thread 1獲取結果后繼續執行.
具體參見如下代碼:
#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;
}
輸出結果

分析
主程序中調用std::async異步調用test函數,可以看到main函數的線程ID 27428與test函數執行的線程ID 9704并不一樣,說明std::async另起了一個新的線程。在test線程中,先sleep 1000ms,所以可以看到"After lanuch a thread:"先輸出,說明主線程異步執行,不受子線程影響。而"After get result "最后輸出,說明get()方法會阻塞主線程,直到獲取結果。
作者:robot2017
出處:http://www.rzrgm.cn/stephen2023/p/18416810
版權:本文版權歸作者和博客園共有
轉載:歡迎轉載,但未經作者同意,必須保留此段聲明;必須在文章中給出原文連接;否則必究法律責任
出處:http://www.rzrgm.cn/stephen2023/p/18416810
版權:本文版權歸作者和博客園共有
轉載:歡迎轉載,但未經作者同意,必須保留此段聲明;必須在文章中給出原文連接;否則必究法律責任
浙公網安備 33010602011771號