当前位置: 首页 > news >正文

C++并发多线程--std::async、std::packaged_task和std::promise的使用

目录

1--std::async的使用

2--std::packaged_task的使用

3--std::promise的使用


1--std::async的使用

        std::async用于启动一个异步任务,并返回一个std::future对象;std::future对象里含有异步任务线程入口函数的结果;

        std::launch::deferred 表示调用线程入口函数将会被延迟到 std::future 的 wait() 或 get() 调用,当 wait() 或者 get() 没有被调用时,线程入口函数不会被调用(线程不会被创建);

#include <iostream>
#include <thread>
#include <mutex>
#include <future>class Sample{
public:// 线程入口函数int thread(int value){std::cout << "thread id: " << std::this_thread::get_id() << std::endl;std::chrono::microseconds dura(value); // reststd::this_thread::sleep_for(dura);return 5;}};int main(int argc, char *argv[]){Sample sample;int value = 5000;// std::async用于启动一个异步任务,并返回一个std::future对象// std::future对象里含有异步任务线程入口函数的结果std::cout << "thread id: " << std::this_thread::get_id() << std::endl;std::future<int> result = std::async(&Sample::thread, &sample, value);// std::launch::deferred 表示调用线程入口函数将会被延迟到 std::future 的wait()或get()调用// 当wait()或者get()没有被调用时,线程入口函数不会被调用(线程不会被创建)// std::future<int> result = std::async(std::launch::deferred, &Sample::thread, &sample, value);// result.get()等待thread()执行完毕获取结果后,主线程才继续往下执行std::cout << "resule.get(): " << result.get() << std::endl; // result.wait() // 等待线程返回,但不返回结果std::cout << "main thread continue ..." << std::endl;return 0;
}

2--std::packaged_task的使用

        std::packaged_task 用于打包任务,其包装各种可调用对象,方便后续作为线程入口函数;

#include <iostream>
#include <thread>
#include <mutex>
#include <future>// 线程入口函数
int thread(int value){std::cout << "thread id: " << std::this_thread::get_id() << std::endl;std::chrono::microseconds dura(value); // reststd::this_thread::sleep_for(dura);return 5;
}int main(int argc, char *argv[]){// std::packaged_task 用于打包任务,其包装各种可调用对象,方便后续作为线程入口函数std::cout << "thead id: " << std::this_thread::get_id() << std::endl;std::packaged_task<int(int)> mypt(thread);int value = 5000;std::thread thread1(std::ref(mypt), value);thread1.join();std::future<int> result = mypt.get_future();std::cout << "result.get(): " << result.get() << std::endl;return 0;
}

3--std::promise的使用

        std::promise 用于在其他线程中使用某个线程中的值;在下面的实例代码中,thread2 使用了 thread1 中的 result 值;

#include <iostream>
#include <thread>
#include <mutex>
#include <future>// 线程入口函数
int thread(std::promise<int> &tmpp, int clac){clac++;clac *= 10; std::cout << "thread id: " << std::this_thread::get_id() << std::endl;int result = clac;tmpp.set_value(result);return 0;
}void thread_2(std::future<int> &tmpf){auto result2 = tmpf.get();std::cout << "tmpf.get(): " << result2 << std::endl;
}int main(int argc, char *argv[]){// std::promise 用于在某个线程中赋值,并在其他线程中将值取来用std::cout << "thead id: " << std::this_thread::get_id() << std::endl;std::promise<int> prom; int clac = 1;std::thread thread1(thread, std::ref(prom), clac);thread1.join();// 将promise中的值取来用std::future<int> result = prom.get_future();std::thread thread2(thread_2, std::ref(result));thread2.join();return 0;
}

http://www.lryc.cn/news/130341.html

相关文章:

  • opencv-目标追踪
  • 【数据结构】 单链表面试题讲解
  • C++ string类的模拟实现
  • Qt实现简单的漫游器
  • 【c语言】文件操作
  • 【Unity】坐标转换经纬度方法(应用篇)
  • element时间选择器el-date-picter使用disabledDate指定禁用的日期
  • 出学校干了 5 年外包,已经废了
  • day-23 代码随想录算法训练营(19)part09
  • JVM编译优化
  • vue浏览器插件安装-各种问题
  • maven工具-maven的使用-镜像仓库、本地仓、IDEA使用maven
  • Mac鼠标增强工具Smooze Pro
  • 数据结构-单链表(C语言简单实现)
  • .netcore grpc身份验证和授权
  • 分布式 - 服务器Nginx:一小时入门系列之负载均衡
  • Linux学习之基本指令二
  • 神经网络基础-神经网络补充概念-41-梯度的数值逼近
  • tornado在模板中遍历二维数组
  • 前端-初始化Vue3+TypeScript
  • 龙蜥社区安全联盟(OASA)正式成立,启明星辰、绿盟、360 等 23 家厂商重磅加入
  • Flask-SQLAlchemy
  • 大数据bug-sqoop(二:sqoop同步mysql数据到hive进行字段限制。)
  • Windows小记
  • centos安装elasticsearch7.9
  • 221、仿真-基于51单片机的智能啤酒发酵罐多点温度压力水位排水加水检测报警系统设计(程序+Proteus仿真+配套资料等)
  • C语言好题解析(三)
  • OpenCV之remap的使用
  • leetcode 377. 组合总和 Ⅳ
  • C++笔记之花括号和圆括号初始化区别,列表初始化和初始化列表区别