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

c++ async 使用详解,创建异步任务的多种方法

c++ async 使用详解

std::async

  • 头文件 #include <future>

  • 函数原型:

    template<class Function, class... Args>
    std::future<std::invoke_result_t<std::decay_t<Function>,std::decay_t<Args>...>>async(std::launch policy, Function&& f, Args&&... args);template<class Function, class... Args>
    std::future<std::invoke_result_t<std::decay_t<Function>,std::decay_t<Args>...>>async(Function&& f, Args&&... args);
    // 等同于以 std::launch::async | std::launch::deferred 策略调用上面的函数
    
    • policy:以何种策略调用可调用对象 f,可以为以下三种:
      • std::launch::async:以异步的方式调用 f,即必须另外开启专属的线程,在其上运行 f。
      • std::launch::deferred:在返回的 std::future 上调用了非定时等待函数,即 wait 或者 get 时,才执行 f。
      • std::launch::async | std::launch::deferred:可能异步运行 f 或者直到调用 wait 或者 get 时才运行,取决于系统的负载,无法人为控制。
    • f:要调用的可调用对象。
    • args:传递给 发的参数。
    • 返回值:std::future 对象,在其上调用 wait 可以等待 f 完成,调用 get 可以等待并获取 f 的返回值。
  • 作用:以异步或者同步的调用可调用对象 f,并可以通过返回的 std::future 对象获取 f 的返回值。

使用注意

  • 如果没有任何对象接收 std::sync 的返回值,即使指定了 std::launch::async 策略,std::future 的析构函数也会阻塞直到整个调用完成。

  • 示例如下:

    // 临时量即 std::future 的析构函数等待睡眠完成。
    std::async(std::launch::async, []{ std::this_thread::sleep_for(10ms); });
    // 在睡眠完成之前,本行代码不会得到运行
    std::async(std::launch::async, []{ printf("xx"); });
    
  • 只有从 std::async 获取的 std::future 对象其析构函数会阻塞,以其他方式获得的 std::future 对象则不会。

示例1 函数作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>int f1(int n, int& b)
{for (int i = 0; i < 3; ++i) {printf("async is running\n");++n;++b;std::this_thread::sleep_for(std::chrono::milliseconds(10));}printf("n is %d, b is %d\n", n, b);return n + b;
}int main()
{int n = 0;int b = 0;// n 按值传递,b 按引用传递auto f = std::async(std::launch::async, f1, n, std::ref(b));int ret = f.get();printf("result is %d\n", ret);return 0;
}

示例2 lamba 作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>int main()
{auto f = std::async([]() -> int {for (int i = 0; i < 3; ++i) {printf("async is running\n");std::this_thread::sleep_for(std::chrono::milliseconds(10));}return 100;});int ret = f.get();printf("result is %d\n", ret);return 0;
}

示例3 std::function 作为参数

#include <stdio.h>
#include <thread>
#include <future>
#include <chrono>
#include <functional>int main()
{std::function<int(void)> func = []() -> int {for (int i = 0; i < 3; ++i) {printf("async is running\n");std::this_thread::sleep_for(std::chrono::milliseconds(10));}return 100;};auto f = std::async(func);int ret = f.get();printf("result is %d\n", ret);return 0;
}
http://www.lryc.cn/news/224689.html

相关文章:

  • 万物皆数——用matlab求解二阶微分方程
  • jmeter接口自动化部署jenkins教程
  • 前端js实现将数组对象组装成自己需要的属性,或者去掉对象中不必要的属性
  • MeterSphere 任意文件读取漏洞(CVE-2023-25814)
  • 设计模式-01-单例设计模式
  • 霍尔电流传感器如何进行可靠性测试?主要应用在哪些领域?
  • pandas按行按列遍历Dataframe的三种方式
  • Api接口如何防止被刷?
  • Django——orm模块创建表关系
  • Django知识点
  • 基于单片机设计的智能风扇(红外线无线控制开关调速定时)
  • k8s报错pause 3.2 解决方案
  • 基于遗传算法的电器分类,基于GA的电器分类
  • 某XX自考小程序的AES加密分析
  • 加密算法笔记
  • Sa-Token拦截全部接口必须登录-然后自定义注解来匿名登录-作为权限框架支持,并且同时使用了注解和路由的拦截器模式,此部分的配置如下:
  • 公司企业端口映射
  • gitlab安装和使用
  • 【论文阅读】DALL·E: Zero-Shot Text-to-Image Generation
  • 说一下 toRef、toRefs,以及他们的区别
  • 修改Android Studio默认的gradle目录
  • 鲁大师电动车智能化测评报告第二十三期:实测续航95km,九号Q90兼顾个性与实用
  • 初始化项目骨架(Web3项目一实战之一)
  • 在opencv OpenCV中打开相机摄像头,用分水岭算法实时实现图像的分割与提取
  • CodeWhisperer 的正确使用
  • selenium xpath定位
  • 「我在淘天做技术」音视频技术及其在淘宝内容业务中的应用
  • el-input 输入后失去焦点
  • docker创建并访问本地前端
  • 数据结构之单链表基本操作