網(wǎng)站首頁 編程語言 正文
No.1 async:創(chuàng)建執(zhí)行線程
1.1 帶返回值的普通線程函數(shù)
第一步: 采用async:?jiǎn)?dòng)一個(gè)異步任務(wù),(創(chuàng)建線程,并且執(zhí)行線程處理函數(shù)),返回值future對(duì)象
第二步: 通過future對(duì)象中的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í)候才會(huì)執(zhí)行,本質(zhì)是沒有創(chuàng)建子線程的
No.2 thread:創(chuàng)建線程
2.1 packaged_task: 打包線程處理函數(shù)
- 通過類模板 packaged_task 包裝線程處理函數(shù)
- 通過packaged_task的對(duì)象調(diào)用get_future獲取future對(duì)象,再通過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存儲(chǔ)函數(shù)需要返回的值
第二步: 通過get_future()獲取future對(duì)象,再通過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; }
原文鏈接:https://blog.csdn.net/m0_50597798/article/details/126710056
相關(guān)推薦
- 2022-04-07 C#實(shí)現(xiàn)Socket服務(wù)器及多客戶端連接的方式_C#教程
- 2023-03-25 Pandas庫中iloc[]函數(shù)的使用方法_python
- 2022-03-12 深入了解C#多線程安全_C#教程
- 2022-04-27 剖析后OpLog訂閱MongoDB的數(shù)據(jù)變更就沒那么難了_MongoDB
- 2022-02-15 小程序搜索框歷史記錄,去除重復(fù)搜索內(nèi)容,限制顯示條數(shù)
- 2022-04-21 詳解前端到底可以用nginx做什么_nginx
- 2022-12-03 C?++迭代器iterator在string中使用方法介紹_C 語言
- 2021-12-10 k8s部署ingress-nginx的方法步驟_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支