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

一个std::async的示例

目录

一、问题引出

二、关键点解释

1.生成随机数

2.异步启动两个操作

3.检查异步任务是否为延迟执行并轮询任务状态

4.等待所有任务完成并处理异常

三、总结


一、问题引出

从《c++标准库》(第2版)看到一个std::async的例子。演示了使用 std::async 和std::future的异步任务执行。

#include <future>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
#include <exception>
using namespace std;

void doSomething (char c)
{
    // random-number generator (use c as seed to get different sequences)
    default_random_engine dre(c);
    uniform_int_distribution<int> id(10,1000);
 
    // loop to print character after a random period of time
    for (int i=0; i<10; ++i) {
        this_thread::sleep_for(chrono::milliseconds(id(dre)));
        cout.put(c).flush();
    }
}

int main()
{
    cout << "starting 2 operations asynchronously" << endl;

    // start two loops in the background printing characters . or +
    auto f1 = async([]{ doSomething('.'); });
    auto f2 = async([]{ doSomething('+'); });

    // if at least one of the background tasks is running
    if (f1.wait_for(chrono::seconds(0)) != future_status::deferred ||
        f2.wait_for(chrono::seconds(0)) != future_status::deferred) {
        // poll until at least one of the loops finished
        while (f1.wait_for(chrono::seconds(0)) != future_status::ready &&
               f2.wait_for(chrono::seconds(0)) != future_status::ready) {
            //...;
            this_thread::yield();  // hint to reschedule to the next thread
        }
    }
    cout.put('\n').flush();

    // wait for all loops to be finished and process any exception
    try {
        f1.get();
        f2.get();
    }
    catch (const exception& e) {
        cout << "\nEXCEPTION: " << e.what() << endl;
    }
    cout << "\ndone" << endl;
}

二、关键点解释

1.生成随机数

  • default_random_engine dre(c);:创建一个默认的随机数生成器 dre,并使用字符 c 作为种子。这样不同的调用会得到不同的随机数序列。

  • uniform_int_distribution<int> id(10,1000);:创建一个均匀整数分布对象 id,用于生成范围在 10 到 1000 之间的随机整数。

2.异步启动两个操作

auto f1 = async([]{ doSomething('.'); });
auto f2 = async([]{ doSomething('+'); });
  • std::async 是一个用于异步执行任务的函数模板。它会启动一个新的线程(或者使用线程池中的线程)来执行传入的可调用对象(这里是 lambda 表达式)。

  • f1 和 f2 是 std::future 对象,用于获取异步操作的结果。这里的异步操作是调用 doSomething 函数,分别传入字符 . 和 +

3.检查异步任务是否为延迟执行并轮询任务状态

if (f1.wait_for(chrono::seconds(0)) != future_status::deferred ||
    f2.wait_for(chrono::seconds(0)) != future_status::deferred) {
    while (f1.wait_for(chrono::seconds(0)) != future_status::ready &&
           f2.wait_for(chrono::seconds(0)) != future_status::ready) {
        this_thread::yield();  // hint to reschedule to the next thread
    }
}
cout.put('\n').flush();
  • f1.wait_for(chrono::seconds(0)) 和 f2.wait_for(chrono::seconds(0)):检查 f1 和 f2 对应的异步任务的状态,chrono::seconds(0) 表示不等待,立即返回任务状态。

  • future_status::deferred 表示任务是延迟执行的。如果至少有一个任务不是延迟执行的,则进入内层循环。

  • 内层 while 循环会不断检查 f1 和 f2 的状态,直到至少有一个任务完成(状态为 future_status::ready)。

  • this_thread::yield();:提示操作系统将当前线程的执行权让给其他线程,避免忙等待。

4.等待所有任务完成并处理异常

try {
    f1.get();
    f2.get();
}
catch (const exception& e) {
    cout << "\nEXCEPTION: " << e.what() << endl;
}
  • f1.get() 和 f2.get():阻塞当前线程,直到 f1 和 f2 对应的异步任务完成,并获取任务的结果。如果任务抛出异常,get() 函数会重新抛出该异常。

  • catch (const exception& e):捕获可能抛出的异常,并输出异常信息。

三、总结

这段代码的主要功能是异步启动两个任务,每个任务会随机暂停一段时间后输出一个字符,程序会等待至少一个任务完成,最后等待所有任务完成并处理可能的异常。


http://www.kler.cn/a/561459.html

相关文章:

  • 【Linux进程三】进程的状态
  • XTOM工业级蓝光三维扫描仪在笔记本电脑背板模具全尺寸检测中的高效精准应用
  • 001第一个flutter文件
  • 源码分享1:批量修改PDF文件名称
  • 【MySQL篇】MySQL操作库
  • LIS系统源码,医院检验系统源码,lis软件源码
  • Java Web开发实战与项目——项目集成与部署
  • 1 什么是微服务?MybatisPlus框架
  • NVIDIA DLI引领创新课程:基于提示工程的LLM应用开发探索
  • 如何在Ubuntu 22.04或20.04 Linux上安装MobaXterm
  • 力扣2454. 下一个更大元素 IV
  • 电脑键盘知识
  • 【视频2 - 4】初识操作系统,Linux,虚拟机
  • windows断网,提示无法自动将ip协议堆栈绑定到网络适配器的解决办法
  • Oracle 数据泵迁移步骤规范
  • 双周报Vol.66: String模式匹配增强、while条件支持使用 is 表达式、新增IDE安装器...多项核心技术更新!
  • ui设计公司兰亭妙微分享:科研单位UI界面设计
  • 【C# 变量字符串还原转义字符】
  • TensorFlow深度学习实战(9)——构建VGG模型实现图像分类
  • LeetCode2587