C++多線程之帶返回值的線程處理函數(shù)解讀
No.1 async:創(chuàng)建執(zhí)行線程
1.1 帶返回值的普通線程函數(shù)
第一步: 采用async:啟動(dòng)一個(gè)異步任務(wù),(創(chuàng)建線程,并且執(zhí)行線程處理函數(shù)),返回值future對象
第二步: 通過future對象中的get()方法獲取線程返回值
#include <iostream> #include <thread> #include <future> using namespace std; int testThreadOne() { cout<<"testThreadOne_id:"<<this_thread::get_id()<<endl; return 1001; } void testAsync1() { future<int> result = async(testThreadOne); cout<<result.get()<<endl; }
1.2 帶返回值的類成員函數(shù)
class Tihu { public: int TihuThread(int num) { cout<<"TihuThread_id"<<this_thread::get_id()<<endl; num *= 2; this_thread::sleep_for(2s); return num; } } void testAsync2() Tihu tihu; future<int> result = async(&Tihu::TihuThread,&tihu,1999); cout<<result.get()<<endl; }
1.3 async的其他參數(shù)
launch::async
: 創(chuàng)建線程并且執(zhí)行線程處理函數(shù)launch::deferred
:線程處理函數(shù)延遲到 我們調(diào)用wait和get方法的時(shí)候才會執(zhí)行,本質(zhì)是沒有創(chuàng)建子線程的
No.2 thread:創(chuàng)建線程
2.1 packaged_task: 打包線程處理函數(shù)
- 通過類模板 packaged_task 包裝線程處理函數(shù)
- 通過packaged_task的對象調(diào)用get_future獲取future對象,再通過get()方法得到子線程處理函數(shù)的返回值
void testPackaged_task() { //1. 打包普通函數(shù) packaged_task<int(void)> taskOne(testThreadOne);//函數(shù)返回值加上參數(shù)類型 thread testOne(ref(taskOne));//需要使用ref轉(zhuǎn)換 testOne.join(); cout<<taskOne.get_future().get()<<endl; //2. 打包類中的成員函數(shù) //需要使用函數(shù)適配器進(jìn)行封裝 Tihu tihu; packaged_task<int(int)> taskTwo(bind(&Tihu::TihuThread,&tihu,placeholders::_1));//如果有參數(shù)需要使用占位符 thread testTwo(ref(taskTwo),20); testTwo.join(); cout<<testTwo.get_future().get()<<endl; //3. 打包Lambda表達(dá)式 packaged_task<int(int)> taskThree([](int num) { cout<<"thread_id:"<<this_thread::get_id()<<endl; num *= 10; return num; }); thread testTwo(ref(taskThree),7); testTwo.join(); cout<<testTwo.get_future().get()<<endl; }
2.2 promise: 獲取線程處理函數(shù)“返回值”
第一步: promise類模板,通過調(diào)用set_value存儲函數(shù)需要返回的值
第二步: 通過get_future()獲取future對象,再通過get()方法獲取線程處理函數(shù)中的值
void testPromiseThread(promise<int>& temp,int data) { ?? ?cout<<"testPromise"<<this_thread::get_id()<<endl; ?? ?data *= 3; ?? ?temp.set_value(data); } void testPromise() { ?? ?promise<int> temp; ?? ?thread testp(testPromiseThread,ref(temp),19); ?? ?testp.join(); ?? ?cout<<temp.get_future().get()<<endl; }
int main() { ?? ?testAsync1(); ?? ?testAsync2(); ?? ?testPackaged_task(); ?? ?testPromise(); ?? ? ?? ?return 0; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV實(shí)現(xiàn)特征檢測和特征匹配方法匯總
一幅圖像中總存在著其獨(dú)特的像素點(diǎn),這些點(diǎn)我們可以認(rèn)為就是這幅圖像的特征,成為特征點(diǎn),本文主要介紹了OpenCV實(shí)現(xiàn)特征檢測和特征匹配方法,感興趣的可以了解一下2021-08-08C語言如何利用輾轉(zhuǎn)相除法求最大公約數(shù)
這篇文章主要介紹了C語言如何利用輾轉(zhuǎn)相除法求最大公約數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Qt5實(shí)現(xiàn)qDebug日志信息寫入日志文件過程
這篇文章主要為大家介紹了Qt5實(shí)現(xiàn)qDebug日志信息寫入日志文件的過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Windows下VScode實(shí)現(xiàn)簡單回聲服務(wù)的方法
回聲服務(wù)端可以將客戶端傳來的信息,再原封不動(dòng)地發(fā)送給客戶端,因而得名 epoch 服務(wù)。接下來通過本文給大家介紹Windows下VScode實(shí)現(xiàn)簡單回聲服務(wù)的方法,感興趣的朋友一起看看吧2021-08-08C++ boost::asio編程-同步TCP詳解及實(shí)例代碼
這篇文章主要介紹了C++ boost::asio編程-同步TCP詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11C語言用函數(shù)指針實(shí)現(xiàn)一個(gè)特別的計(jì)算器
函數(shù)指針是一個(gè)指針變量,它可以存儲函數(shù)的地址,然后使用函數(shù)指針,下面這篇文章主要給大家介紹了關(guān)于C語言用函數(shù)指針實(shí)現(xiàn)計(jì)算器的方法,需要的朋友可以參考下2022-07-07C++拷貝構(gòu)造函數(shù)和賦值運(yùn)算符重載詳解
拷貝構(gòu)造函數(shù)是特殊的構(gòu)造函數(shù),是用一個(gè)已經(jīng)存在的對象,賦值拷貝給另一個(gè)新創(chuàng)建的已經(jīng)存在的對象,這篇文章主要介紹了C++拷貝構(gòu)造函數(shù)和賦值運(yùn)算符重載,需要的朋友可以參考下2024-05-05C++控制臺實(shí)現(xiàn)隨機(jī)生成路徑迷宮游戲
這篇文章主要為大家詳細(xì)介紹了C++控制臺實(shí)現(xiàn)隨機(jī)生成路徑迷宮游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05