C++ 异步 async future 等

news/2024/9/22 13:11:42

async 和 future

这个和 C# 的 Task 有点像。

#include <iostream>
#include <string>
#include <memory>
#include <future>
#include <thread>using namespace std;int calculate()
{std::this_thread::sleep_for(std::chrono::seconds(2));return 42;
}class Dog
{
public:int wangWang(){std::this_thread::sleep_for(std::chrono::seconds(3));return 23;}
};int main()
{std::future<int> res = std::async(std::launch::async, calculate);int result = res.get(); // 这里会等待完成cout << result << '\n';Dog dog;res = std::async(std::launch::async, &Dog::wangWang, &dog); // 成员函数int dog_result = res.get();cout << dog_result << '\n';return EXIT_SUCCESS;
}

输出:

42
23

std::packaged_task

#include <iostream>
#include <memory>
#include <future>
using namespace std;int calculate(int x, int y)
{return x + y;
}class Dog
{
public:int calculate(int x, int y){return x + y;}
};int main()
{// 直接绑定函数std::packaged_task<int(int, int)> task(calculate);std::future<int> f_result = task.get_future();std::thread t(std::move(task), 100, 200);t.join();int result = f_result.get();cout << result << '\n';// bind 类成员函数 可调用对象Dog dog;std::function<int(int, int)> func = std::bind(&Dog::calculate, &dog, std::placeholders::_1, std::placeholders::_2);std::packaged_task<int(int, int)> task_1(func);std::future<int> f_result_1 = task_1.get_future();std::thread t_1(std::move(task_1), 10, 20);t_1.join();int result_1 = f_result_1.get();cout << result_1 << '\n';system("pause");return EXIT_SUCCESS;
}

输出:

300
30

promise

能够在某个线程中给它赋值,然后我们可以在其他线程中,把这个取值出来用;

#include <future>
#include <iostream>
int main()
{std::promise<int> p;std::future<int> f = p.get_future();std::thread t([&p](){p.set_value(42);});t.join();int result = f.get();std::cout << result << std::endl; // 输出42system("pause");return EXIT_SUCCESS;
}




参考:http://www.seestudy.cn/?list_9/43.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/63316.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Nuxt Kit API :路径解析工具

title: Nuxt Kit API :路径解析工具 date: 2024/9/22 updated: 2024/9/22 author: cmdragon excerpt: 摘要:本文介绍了Nuxt Kit中用于解析路径的API工具,包括resolvePath、resolveAlias、findPath和createResolver。这些工具助力开发者处理模块路径、别名、文件扩展名,提…

使用GPU 加速 Polars:高效解决大规模数据问题

Polars 最近新开发了一个可以支持 GPU 加速计算的执行引擎。这个引擎可以对超过 100GB 的数据进行交互式操作能。本文将详细讨论 Polars 中DF的概念、GPU 加速如何与 Polars DF协同工作,以及使用新的 CUDA 驱动执行引擎可能带来的性能提升。 https://avoid.overfit.cn/post/b…

我的网站集成ElasticSearch初体验

最近,我给我的网站(https://www.xiandanplay.com/)尝试集成了一下es来实现我的一个搜索功能,因为这个是我第一次了解运用elastic,所以如果有不对的地方,大家可以指出来,话不多说,先看看我的一个大致流程 这里我采用的sdk的版本是Elastic.Clients.Elasticsearch, Ver…

Flipper Zero极客的便携式多功能工具设备

官网:Flipper Zero — 极客的便携式多功能工具设备 Flipper Zero是近两年比较热门的硬件工具,官方固件主要涵盖的功能为Sub-Ghz,125kHz,NFC,红外。 基本信息资料都可以在官方网站找到比较详细的文档解释。本篇主要是一个基础入门,这系列也是给自己学习此硬件一个上手研究…

C盘扩容免费工具

1.diskgenius 下载 https://www.diskgenius.cn/download.php 解压即可使用,无需安装 2.下载 安装Windows_PE环境 https://www.diskgenius.cn/help/windows_aik_adk_installnotes.php?Version=0A000000&Build=22631&Lang=936 官方软件,安全五毒 3.运行diskgenius ,点…

Leetcode 2464. 有效分割中的最少子数组数目

1.题目基本信息 1.1.题目描述 给定一个整数数组 nums。 如果要将整数数组 nums 拆分为 子数组 后是 有效的,则必须满足: 每个子数组的第一个和最后一个元素的最大公约数 大于 1,且 nums 的每个元素只属于一个子数组。 返回 nums 的 有效 子数组拆分中的 最少 子数组数目。如果…

debian 系统服务器搭建

在VMWare中安装Debian12.5虚拟机后, 需要开始进行一些设置:1. 先设置网络模式为桥接模式, 这样和主机在同一个局域网,方便后续ssh连接 2. 第1步设置后,重启Debian,登录后, 查看IP和Mac地址, 192.168.31.16,00:0c:29:6c:31:e6 3. 设置路由器,固定IP: 192.168.31.105。 …

初识算法

持续更新数据结构与算法专题,欢迎关注.......1.1 什么是算法? 定义 在数学和计算机科学领域,算法是一系列有限的严谨指令,通常用于解决一类特定问题或执行计算In mathematics and computer science, an algorithm (/ˈlɡərɪəm/) is a finite sequence of rigorous inst…