当前位置: 首页> 汽车> 维修 > pageadmin自助建站_dwcs6中文破解版下载_百度云网盘资源_网络广告营销的典型案例

pageadmin自助建站_dwcs6中文破解版下载_百度云网盘资源_网络广告营销的典型案例

时间:2025/7/9 21:33:00来源:https://blog.csdn.net/weixin_41554427/article/details/145864777 浏览次数: 0次
pageadmin自助建站_dwcs6中文破解版下载_百度云网盘资源_网络广告营销的典型案例

目录

一、问题引出

二、关键点解释

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 timefor (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 runningif (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 finishedwhile (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 exceptiontry {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):捕获可能抛出的异常,并输出异常信息。

三、总结

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

关键字:pageadmin自助建站_dwcs6中文破解版下载_百度云网盘资源_网络广告营销的典型案例

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: