[Modern C++]现代C++中优雅的异步操作

news/2024/6/30 23:21:52

背景

现代C++提供了一些很好的特性可以让我们实现优雅、高效的异步操作,今天向大家简要地介绍一下。

std::async

std::async(std::launch::async | std::launch::deferred, f, args...)
  • f为需要异步执行的函数,

  • args为f函数的参数,

  • 第一个参数可选,在Visiual C++中默认为std::launch::async,即创建的future对象在创建完成后立即异步执行,当第一个参数为std::launch::deferred时表示future对象延迟执行。

样例

代码

#include <iostream>
#include <future>
#include <thread>
#include <chrono>
using namespace std;
int main(){
    auto future1 = async([](int &&x)->int{
        auto ret = 0;
        for(auto i = 0; i < x; i++){
            this_thread::sleep_for(chrono::seconds(1));
            ret += i;
            cout << "Task 1 calculating to "  << x - 1 << " and now the turn is " << i << " and the value is " << ret << endl;
        }
        return ret;
    },10);    //future1 立即异步执行
    auto future2 = async([](int &&x)->int{
        auto ret = 1;
        for(auto i = 1; i < x + 1; i++){
            this_thread::sleep_for(chrono::seconds(2));
            ret *= i;
            cout << "Task 2 calculating to "  << x << " and now the turn is " << i << " and the value is " << ret << endl;
        }
        return ret;
    },5);     //future2 立即异步执行
    std::cout << "waiting...\n";
    cout << "Result of task 1: " << future1.get() << endl;
    cout << "Result of task 2: " << future2.get() << endl;
}

输出

c:\Users\yixian\workspace\test>async
waiting...
Task 1 calculating to 9 and now the turn is 0 and the value is 0
Task 1 calculating to 9 and now the turn is 1 and the value is 1
Task 2 calculating to 5 and now the turn is 1 and the value is 1
Task 2 calculating to 5 and now the turn is 2 and the value is 2
Task 2 calculating to 5 and now the turn is 3 and the value is 6Task 1 calculating to 9 and now the turn is 2 and the value is 3
Task 1 calculating to 9 and now the turn is 3 and the value is 6
Task 1 calculating to 9 and now the turn is 4 and the value is 10
Task 1 calculating to 9 and now the turn is 5 and the value is 15
Task 1 calculating to 9 and now the turn is 6 and the value is 21
Task 1 calculating to 9 and now the turn is 7 and the value is 28
Task 1 calculating to 9 and now the turn is 8 and the value is 36
Task 1 calculating to 9 and now the turn is 9 and the value is 45

Task 2 calculating to 5 and now the turn is 4 and the value is 24
Task 2 calculating to 5 and now the turn is 5 and the value is 120
Result of task 1: 45

指定参数为std::launch::deffered

#include <iostream>
#include <future>
#include <thread>
#include <chrono>
using namespace std;
int main(){
    auto future1 = async(launch::deferred,[](int &&x)->int{
        auto ret = 0;
        for(auto i = 0; i < x; i++){
            this_thread::sleep_for(chrono::seconds(1));
            ret += i;
            cout << "Task 1 calculating to "  << x - 1 << " and now the turn is " << i << " and the value is " << ret << endl;
        }
        return ret;
    },10);    //future1 并不会立即异步执行
    auto future2 = async(launch::deferred,[](int &&x)->int{
        auto ret = 1;
        for(auto i = 1; i < x + 1; i++){
            this_thread::sleep_for(chrono::seconds(2));
            ret *= i;
            cout << "Task 2 calculating to "  << x << " and now the turn is " << i << " and the value is " << ret << endl;
        }
        return ret;
    },5);    //future2 并不会立即异步执行
    std::cout << "waiting...\n";
    cout << "Result of task 1: " << future1.get() << endl; //future1在此时执行
    cout << "Result of task 2: " << future2.get() << endl; //futute2在此时执行
}

输出

c:\Users\yixian\workspace\test>async
waiting...
Task 1 calculating to 9 and now the turn is 0 and the value is 0
Task 1 calculating to 9 and now the turn is 1 and the value is 1
Task 1 calculating to 9 and now the turn is 2 and the value is 3
Task 1 calculating to 9 and now the turn is 3 and the value is 6
Task 1 calculating to 9 and now the turn is 4 and the value is 10
Task 1 calculating to 9 and now the turn is 5 and the value is 15
Task 1 calculating to 9 and now the turn is 6 and the value is 21
Task 1 calculating to 9 and now the turn is 7 and the value is 28
Task 1 calculating to 9 and now the turn is 8 and the value is 36
Task 1 calculating to 9 and now the turn is 9 and the value is 45
Result of task 1: 45
Task 2 calculating to 5 and now the turn is 1 and the value is 1
Task 2 calculating to 5 and now the turn is 2 and the value is 2
Task 2 calculating to 5 and now the turn is 3 and the value is 6
Task 2 calculating to 5 and now the turn is 4 and the value is 24
Task 2 calculating to 5 and now the turn is 5 and the value is 120
Result of task 2: 120

http://www.niftyadmin.cn/n/3122538.html

相关文章

huawei 三套题

链接&#xff1a;https://www.nowcoder.com/questionTerminal/fe298c55694f4ed39e256170ff2c205f 有这样一道智力题&#xff1a;“某商店规定&#xff1a;三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶&#xff0c;她最多可以换多少瓶汽水喝&#xff1f;”答案是5瓶&…

让两个数x,y一直保持互质的模版

1 int gcd(int x,int y) 2 { 3 if(y0)return x; 4 else return gcd(y,x%y); 5 } 转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/6390160.html

oracle 建立表序列

1.Table(tableName "orderlogs", seqName "orderlogs_seq") //表关联 2.CREATE SEQUENCE orderlogs_seq //建序列 3.select orderlogs_seq.nextval from dual; //查序列大小 4.ALTER SEQUENCE orderlogs_seq INCREMENT BY 48000; //增加序列大小转载于…

Linux下利用signal函数处理ctrl+c等信号

前言 linux下能够通过信号机制来实现程序的软中断&#xff0c;是一个很实用的编程方法。我们平时在程序执行的时候按下ctrl-c、ctrl-z或者kill一个进程的时候事实上都等效于向这个进程发送了一个特定信号&#xff0c;当进程捕获到信号后&#xff0c;进程会被中断并马上跳转到信…

win10开始菜单添加磁贴_Win10 开始菜单全新设计再曝光,磁贴图标出现彩虹选中效果...

自从Windows 10发布以来&#xff0c;就已经有不计其数的设计师或者是开发爱好者针对于Win 10进行了许多字形的设计以及改进&#xff0c;尤其是在外观设计UI上。而在此之前的2017开发者大会上&#xff0c;微软也是推出了一套全新的设计语言系统&#xff0c;也就是Fluent Design …

Codeforces Round #401 (Div. 1) C(set+树状数组)

题意&#xff1a; 给出一个序列&#xff0c;给出一个k&#xff0c;要求给出一个划分方案&#xff0c;使得连续区间内不同的数不超过k个&#xff0c;问划分的最少区间个数&#xff0c;输出时将k1~n的答案都输出 比赛的时候想的有点偏&#xff0c;然后写了个nlog^2n的做法&#x…

JSP是什么?

JSP是什么&#xff1f;sun公司制定的一种服务器端动态页面技术规范。JSP其实是一个以“jsp”为后缀的文件&#xff0c;该文件的内容主要是html和少量的java代码&#xff0c;容器会将jsp文件自动转换成一个servlet然后执行。如何写一个JSP文件&#xff1f;step1,创建一个以“.js…

python数据科学手册pdf微盘_适合新手的Python数据科学

对于做数据工作的新手&#xff0c;学习和使用一门编程语言&#xff0c;是基本的要求。你可以根据自己的实际情况&#xff0c;选择适合自己的编程语言。做数据工作的朋友&#xff0c;有的使用R语言(我的很多数据工作就是用R语言完成)&#xff0c;有的使用Python语言(我也是用Pyt…