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

推荐扬中网站建设网络优化工程师前景

推荐扬中网站建设,网络优化工程师前景,自建网站备案通过后怎么做,长沙景观设计公司提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言1、std::future和std::shared_future1.1 std:future1.2 std::shared_future2、std::async3、std::promise4、std::packaged_task前言 C11提供了异步操作相关的类…

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 1、std::future和std::shared_future
    • 1.1 std:future
    • 1.2 std::shared_future
  • 2、std::async
  • 3、std::promise
  • 4、std::packaged_task

前言

C++11提供了异步操作相关的类,主要有std:future、std:promise 和std:package task。std:future作为异步结果的传输通道,可以很方便地获取线程函数的返回值;。std:promise 用来包装一个值、将数据和future绑定起来,方便线程赋值; std:package_task 用来包装一个可调用对象,将函数和future绑定起来,以便异步调用。

1、std::future和std::shared_future

1.1 std:future

C++11中增加的线程,使得我们可以非常方便地创建和使用线程,但有时会有些不便,比如希望获取线程函数的返回结果,就不能直接通过thread.join()得到结果,这时就必须定义一个变量,在线程函数中去给这个变量赋值,然后执行join(),最后得到结果,这个过程是比较烦琐的。thread 库提供了future用来访问异步操作的结果,因为一个异步操作的结果不能马上获取,只能在未来某个时候从某个地方获取,这个异步操作的结果是一个未来的期待值,所以被称为future,future 提供了获取异步操作结果的通道。我们可以以同步等待的方式来获取结果,可以通过查询future 的状态( future_status) 来获取异步操作的结果。futrue_status有3种状态:

1、Deferred:异步操作还没开始
2、Ready:异步操作已经完成
3、Timeout:异步操作超时

future原型

template <class T>  future;
template <class R&> future<R&>;     
template <>         future<void>

std::future是一个类模板

成员方法
在这里插入图片描述
通过future构造函数构造的future对象是无效的,此时调用future.get()可能会抛异常,需要我们捕获

int main()
{future<int> fu;try{cout << fu.get() << endl;}catch (const std::exception& e){cout << e.what() << endl;}return 0;
}

在这里插入图片描述

如果没有初始化future,则表明它是无效的,此时我们也可以使用valid判断future对象是否有效

int main()
{future<int> fu;if (fu.valid()){fu.get();}else{cout << "no state" << endl;}return 0;
}

std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async

mutex mtx;
string GetCurrentDateTime()
{SYSTEMTIME stime;GetLocalTime(&stime);stringstream ss;unique_lock<mutex> lock(mtx);ss << "[" << this_thread::get_id() << "]" << "current time: " << stime.wYear << "/" << stime.wMonth \<< "/" << stime.wDay << "/" << stime.wHour << "/" << stime.wMinute << "/" << stime.wSecond << endl;this_thread::sleep_for(chrono::seconds(2));return ss.str();
}int main()
{future<string> fu = async(GetCurrentDateTime);//执行一个异步任务来获取当前时间cout << this_thread::get_id() << " : main do something..." << endl;cout << fu.get() << endl;return 0;
}

在这里插入图片描述

上述代码中,main线程通过async创建一个新线程去执行异步任务(获取当前系统时间),在新线程执行异步任务的过程中,main线程可以不必阻塞,但在调用get方法时,如果新线程没有返回,就会被阻塞

我们可以通过wait_for进行超时等待返回结果,如果新线程还没返回,就做其他事情,再重复wait_for

mutex mtx;
string GetCurrentDateTime()
{SYSTEMTIME stime;GetLocalTime(&stime);stringstream ss;unique_lock<mutex> lock(mtx);ss << "[" << this_thread::get_id() << "]" << "current time: " << stime.wYear << "/" << stime.wMonth \<< "/" << stime.wDay << "/" << stime.wHour << "/" << stime.wMinute << "/" << stime.wSecond << endl;this_thread::sleep_for(chrono::seconds(3));//休眠3秒return ss.str();
}int main()
{std::future_status status;future<string> fu = async(GetCurrentDateTime);cout << this_thread::get_id() << " : main do something..." << endl;do{status = fu.wait_for(chrono::milliseconds(500));if (status == std::future_status::deferred){cout << "deferred:异步操作还没开始," << "main do something" << endl;}else if (status == std::future_status::timeout){cout << "timeout:在规定时间内异步操作还未完成," << "main do something" << endl;}else if (status == std::future_status::ready){cout << "ready:异步操作已经完成,当前时间为:" << fu.get();}} while (status != std::future_status::ready);return 0;
}

在这里插入图片描述

上述代码中,main线程调用了wait_for(chrono::milliseconds(500)),表示mian线程等待500ms,如果在500ms内,子线程完成异步任务,则立刻返回,如果超过500ms还没有完成异步任务,则不会继续等待,立刻返回

future不允许直接赋值或者拷贝,但允许移动赋值

void func(){}int main()
{future<void> fu1;future<void> fu2 = async(std::launch::async, func);if (fu2.valid()){cout << "fu2.valid()=true" << endl;cout << "move..." << endl;fu1 = std::move(fu2);cout << "fu2.valid()=" <<fu2.valid() << endl;cout << "fu1.valid()=" << fu1.valid() << endl;}else{cout << "fu2.valid()=false" << endl;}return 0;
}

在这里插入图片描述

get成功之后,future对象将不再有效

string func()
{return "fl";
}int main()
{future<string> fu = async(std::launch::async, func);if (fu.valid()){cout << "fu.get() begin fu.valid()=" << fu.valid() << endl;cout << "fu.get()=" << fu.get() << endl;cout << "fu.get() after fu.valid()=" << fu.valid() << endl;}return 0;
}

在这里插入图片描述

1.2 std::shared_future

shared_futrue原型

template <class T>  shared_future;
template <class R&> shared_future<R&>; 
template <>         shared_future<void>; 

C++11中的std: :shared_future是个模板类。与std::future类似,std::shared_future提供了一种访问异步操作结果的机制。不同于std::future,std::shared_futrue允许多个线程等待同一个共享状态。不同于std::future仅支持移动操作,std::shared_future既支持移动操作也支持拷贝操作,而多个std::shared_future对象可以引用相同的共享状态

前面谈到,对于std::future对象,如果就绪,使用get后,future对象就无效了,如果再使用get,则会抛异常。使用std::shared_future就很好的解决了这个问题

string func()
{return "fl";
}int main()
{shared_future<string> fu = async(std::launch::async, func);if (fu.valid()){cout << "fu.get()=" << fu.get() << endl;cout << "fu.get()=" << fu.get() << endl;cout << "fu.get()=" << fu.get() << endl;}else{cout << "fu.valid()=false" << endl;}return 0;
}

在这里插入图片描述

支持拷贝和赋值

string func()
{return "fl";
}int main()
{shared_future<string> fu = async(std::launch::async, func);shared_future<string> fu1 = fu;shared_future<string> fu2(fu1);if (fu.valid()){cout << "fu.get()=" << fu.get() << endl;cout << "fu1.get()=" << fu.get() << endl;cout << "fu2.get()=" << fu.get() << endl;}else{cout << "fu.valid()=false" << endl;}return 0;
}

在这里插入图片描述

2、std::async

std::async比std::future和std::promise、std::packaged和std::thread更高一层,它可以用来直接创建异步的task,异步任务返回的结果也保存在std::future中,需要获取异步任务的结果时,只需要调用future.get()方法即可,如果不关注异步任务的结果,只是简单地等待任务完成时的话,则调用future.wait()方法

async原型

template <class Fn, class... Args>future<typename result_of<Fn(Args...)>::type>async (Fn&& fn, Args&&... args);template <class Fn, class... Args>future<typename result_of<Fn(Args...)>::type>async (launch policy, Fn&& fn, Args&&... args);

后者比前者多了一个参数:launch policy,这个表示线程的创建策略,有两种策略,默认的策略是立即创建线程

  1. std::launch::async:在调用async时就开始创建线程
  2. std::launch::deferred:延迟加载方式创建线程。在调用async时不创建线程,知道调用了future的get或者wait时才创建线程

第二个参数:表示线程的入口函数,必须是一个可调用对象
第三个参数:表示线程函数的参数

std::async的操作,其实相当于封装了std::promise、std::packaged_task加上std::thread

使用std::launch::deferred作为参数

mutex mtx;
string GetCurrentDateTime()
{SYSTEMTIME stime;GetLocalTime(&stime);stringstream ss;unique_lock<mutex> lock(mtx);ss << "[" << this_thread::get_id() << "]" << "current time: " << stime.wYear << "/" << stime.wMonth \<< "/" << stime.wDay << "/" << stime.wHour << "/" << stime.wMinute << "/" << stime.wSecond << endl;cout << "in GetCurrentDateTime()" << endl;return ss.str();
}int main()
{future<string> fu = async(std::launch::deferred, GetCurrentDateTime);this_thread::sleep_for(chrono::milliseconds(500));cout << "main id is " << this_thread::get_id() << endl;cout << fu.get() << endl;return 0;
}

在这里插入图片描述
可以看到GetCurrentDateTime()也是有main线程执行的。在上述的执行顺序中,main线程先打印了自己的线程id,然后调用fu.get()时进入GetCurrentDateTime()内部,完成任务后才打印的时间。因此就能很好的证明deferred参数确实是在创建async时,不创建线程执行任务,而是在调用get方法时,由main线程去执行异步任务。

将参数改为async,执行结果为:

在这里插入图片描述

可以看到,在调用async时就开始创建线程去执行异步任务。

如果我们没有指定第一个参数,默认是async | deferred,具体采用哪个参数,取决于操作系统
在这里插入图片描述

3、std::promise

std::promise将数据和future绑定起来,为获取线程函数中的某个值提供便利,在线程函数中为外面传进来的promise赋值,在线程函数执行完成之后就可以通过promise的future获取该值了。值得注意的是:取值是间接地通过promise内部提供的future来获取的。

std::promise原型

template <class T>  promise;		 //空模板
template <class R&> promise<R&>;     //用于线程间交流对象
template <>         promise<void>;   //用于交流无状态事件

成员方法
在这里插入图片描述

举个例子,简单了解一下promise和future是如何关联的

void func1(std::promise<int>& promObj)
{cout << this_thread::get_id() << "设置promise的值" << endl;promObj.set_value(5);
}void func2(std::future<int>& fut)
{cout << this_thread::get_id() << "访问promise的值" << endl;cout << this_thread::get_id() << ":" << fut.get() << endl;
}int main()
{std::promise<int> prom;std::future<int> fut = prom.get_future();std::thread t1(func1, std::ref(prom));std::thread t2(func2, std::ref(fut));t1.join();t2.join();return 0;
}

在这里插入图片描述

在上述代码中,我们定义一个std::promise<int> prom,并让其与std::future<int> fut进行关联。让t1线程去调用fun11,t2线程去调用func2。如果t2线程在func2中调用fut.get()时,而fut并没有被设置,此时f2线程会被阻塞,直到t1线程在func1中调用promObj.set_value(5),例如:

在这里插入图片描述
std::promise的operator=没有拷贝语义,即std::promise的普通赋值函数是被delete掉了,只有move语义,因此std::promise对象是禁止拷贝的

int main()
{std::promise<int> prom;std::future<int> fut = prom.get_future();std::promise<int> prom1 = prom;return 0;
}

在这里插入图片描述

采用set_value_at_thread_exit演示一下死锁

mutex mtx;
void func1(std::promise<int>& promObj)
{this_thread::sleep_for(chrono::microseconds(500));promObj.set_value_at_thread_exit(1);unique_lock<mutex> lock(mtx);cout << this_thread::get_id() << "设置promise的值" << endl;
}void func2(std::future<int>& fut)
{unique_lock<mutex> lock(mtx);cout << this_thread::get_id() << "访问promise的值" << endl;cout << this_thread::get_id() << ":" << fut.get() << endl;
}int main()
{std::promise<int> prom;std::future<int> fut = prom.get_future();thread t1(func1, std::ref(prom));thread t2(func2, std::ref(fut));t1.join();t2.join();return 0;
}

在这里插入图片描述
为了让func2中的代码先执行,在func1的开始调用了sleep_for(chrono::microseconds(500)),休眠500微秒。首先f2线程拿到锁,执行到fut.get()会被阻塞,因为此时fut还未就绪。线程t1使用set_value_at_thread_exit(1),表明设置指定值为1,但是只有在t1线程结束时,才提醒。因此t1线程将被阻塞在unique_lock lock(mtx)上,而t2线程被阻塞在fut.get()上,所以产生了死锁。

使用set_value_at_thread_exit好处就是确保某个线程的退出,另一个线程才能被获取到某个值,例如:

void func1(std::promise<int>& promObj)
{promObj.set_value(1);cout << this_thread::get_id() << "设置promise的值" << endl;cout << "do something..." << endl;cout << "do something..." << endl;cout << "do something..." << endl;cout << "do something..." << endl;
}void func2(std::future<int>& fut)
{cout << this_thread::get_id() << "访问promise的值" << endl;cout << this_thread::get_id() << ":" << fut.get() << endl;
}int main()
{std::promise<int> prom;std::future<int> fut = prom.get_future();thread t1(func1, std::ref(prom));thread t2(func2, std::ref(fut));t1.detach();t2.join();return 0;
}

在这里插入图片描述
t1线程进行了分离,t2线程先结束,也就导致了main线程结束了,t1线程在后台执行,并没有将"do something…"打印到终端上。因此可以使用set_value_at_thread_exit使t2线程等待t1线程结束

在这里插入图片描述

一个线程将异常传递给另一个线程

void func1(std::promise<void>& p)
{try{cout << this_thread::get_id() << " throw error" << endl;throw exception("this is func1 throw error");}catch (const std::exception& e){p.set_exception_at_thread_exit(std::current_exception());}
}void func2(std::future<void>& fu)
{try{if (fu.valid())fu.get();}catch (const std::exception e){cout << this_thread::get_id() << " error is " << e.what() << endl;}
}int main()
{std::promise<void> p;std::future<void> fu = p.get_future();thread t1(func1, std::ref(p));thread t2(func2, std::ref(fu));t1.join();t2.join();return 0;
}

在这里插入图片描述

4、std::packaged_task

std:packaged _task 包装了一个可调用对象的包装类(如function、lambda expression、bind expression和another function object),将函数和future绑定起来,以便异步调用,它和std:promise在某种程度上有点像,promise保存了一个共享状态的值,而packaged task 保存的是一个函数。

std::packaged_task原型

template <class T> packaged_task;     // undefined
template <class Ret, class... Args> class packaged_task<Ret(Args...)>;

成员函数

在这里插入图片描述

简单案例

int f(int x, int y) { return std::pow(x, y); }void task_lambda()
{std::packaged_task<int(int, int)> task([](int a, int b) {return std::pow(a, b);});std::future<int> result = task.get_future();//auto result = task.get_future();task(2, 2);std::cout << "task_lambda:\t" << result.get() << '\n';
}void task_bind()
{std::packaged_task<int()> task(std::bind(f, 2, 3));std::future<int> result = task.get_future();//auto result = task.get_future();task();std::cout << "task_bind:\t" << result.get() << '\n';
}void task_thread()
{std::packaged_task<int(int, int)> task(f);std::future<int> result = task.get_future();std::thread task_td(std::move(task), 2, 4);task_td.join();std::cout << "task_thread:\t" << result.get() << '\n';
}int main()
{task_lambda();task_bind();task_thread();return 0;
}

在这里插入图片描述


文章转载自:
http://dinncoobtestation.tpps.cn
http://dinncokatangese.tpps.cn
http://dinncobackwardly.tpps.cn
http://dinncoperfidy.tpps.cn
http://dinncodisclosure.tpps.cn
http://dinncointuitionistic.tpps.cn
http://dinncooutline.tpps.cn
http://dinncoanisocoria.tpps.cn
http://dinncowctu.tpps.cn
http://dinncowebbed.tpps.cn
http://dinncofiscal.tpps.cn
http://dinncodzho.tpps.cn
http://dinncoyawnful.tpps.cn
http://dinncoempirical.tpps.cn
http://dinncorassling.tpps.cn
http://dinncoabattis.tpps.cn
http://dinncoejectamenta.tpps.cn
http://dinncowhereto.tpps.cn
http://dinncogiblets.tpps.cn
http://dinncoamen.tpps.cn
http://dinncofuturologist.tpps.cn
http://dinncoincontrollably.tpps.cn
http://dinncoasterid.tpps.cn
http://dinncotactics.tpps.cn
http://dinncoergotin.tpps.cn
http://dinncoroofage.tpps.cn
http://dinncomollusk.tpps.cn
http://dinncoillusiveness.tpps.cn
http://dinncounshunned.tpps.cn
http://dinncounwitting.tpps.cn
http://dinncosuperpower.tpps.cn
http://dinncoextralunar.tpps.cn
http://dinncounwelcome.tpps.cn
http://dinncooveremphasize.tpps.cn
http://dinncoincorruptible.tpps.cn
http://dinncoferal.tpps.cn
http://dinncobantling.tpps.cn
http://dinncosubdiaconate.tpps.cn
http://dinncoextraartistic.tpps.cn
http://dinncocandent.tpps.cn
http://dinncoillusional.tpps.cn
http://dinncoibid.tpps.cn
http://dinncopricket.tpps.cn
http://dinncomuck.tpps.cn
http://dinnconailery.tpps.cn
http://dinncoleviticus.tpps.cn
http://dinncogrammarian.tpps.cn
http://dinncogabriel.tpps.cn
http://dinncoquaalude.tpps.cn
http://dinncostouthearted.tpps.cn
http://dinncoaleyard.tpps.cn
http://dinncofungi.tpps.cn
http://dinncotressure.tpps.cn
http://dinncothermolysin.tpps.cn
http://dinncobrominate.tpps.cn
http://dinncomeditative.tpps.cn
http://dinncoclairschach.tpps.cn
http://dinncofluctuation.tpps.cn
http://dinncolapin.tpps.cn
http://dinncobioactive.tpps.cn
http://dinncoremora.tpps.cn
http://dinncoalgophagous.tpps.cn
http://dinncobalaam.tpps.cn
http://dinncoedulcorate.tpps.cn
http://dinncohistogenic.tpps.cn
http://dinnconavajo.tpps.cn
http://dinncooverword.tpps.cn
http://dinncofaceless.tpps.cn
http://dinncocateran.tpps.cn
http://dinnconameboard.tpps.cn
http://dinncocentralize.tpps.cn
http://dinncocotylosaur.tpps.cn
http://dinncomre.tpps.cn
http://dinncotrichomonacide.tpps.cn
http://dinncoumptieth.tpps.cn
http://dinncoultrasonologist.tpps.cn
http://dinncomicrometeorology.tpps.cn
http://dinncoabloom.tpps.cn
http://dinncodeuteranopic.tpps.cn
http://dinncopallbearer.tpps.cn
http://dinncospittoon.tpps.cn
http://dinncoheaps.tpps.cn
http://dinncombone.tpps.cn
http://dinncosplittism.tpps.cn
http://dinncoepurate.tpps.cn
http://dinncodensitometer.tpps.cn
http://dinncoderation.tpps.cn
http://dinncosalicylate.tpps.cn
http://dinncobuttermilk.tpps.cn
http://dinncoresentful.tpps.cn
http://dinncofomentation.tpps.cn
http://dinncothiophosphate.tpps.cn
http://dinncolavation.tpps.cn
http://dinncobookhunter.tpps.cn
http://dinncogusset.tpps.cn
http://dinncoundissolved.tpps.cn
http://dinncobaseballer.tpps.cn
http://dinncorheotome.tpps.cn
http://dinncogoyische.tpps.cn
http://dinncotriceratops.tpps.cn
http://www.dinnco.com/news/152000.html

相关文章:

  • 网站免费源码大全无用下载韩国网站
  • 做网站却不给客户源代码核心关键词是什么意思
  • 公司网站建设 费用网站统计分析工具的主要功能
  • 微官网站怎么做长沙网络推广
  • 网站建设与网页设计百度文库安徽企业网站建设
  • 旅游业网站开发建设我想在百度上做广告怎么做
  • 用阿里云自己建设网站汕头网站关键词推广
  • 港海(天津)建设股份有限公司网站seo是什么级别
  • 炫酷的动画网站百度seo关键词排名优化教程
  • 南通网络科技有限公司常德网站优化公司
  • 电商网站 设计方案国际新闻最新消息
  • 成都php网站制作程序员培训网站推荐
  • 湖南在建工程查询深圳防疫措施优化
  • 国中建设委员会网站自媒体seo是什么意思
  • 织梦制作html 网站地图种子搜索
  • 上海网站建设百度推广公司哪家好百度推广开户多少钱
  • 重庆网站备案最快几天成都网站建设公司
  • 局域网网站架设软件网络营销概述
  • 流速cms是什么意思googleseo排名公司
  • 超可爱做头像的网站网站推广业务
  • seo网站排名优化软件重庆seo建站
  • 网站百度多久做一次排名电商运营公司排名
  • win7 搭建iss网站长沙官网seo技巧
  • 比较好的网站设计公司短期职业技能培训班
  • 天津住房和城乡建设委员会官方网站地推的60种方法
  • 株洲网站建设的公司怎么找湖北seo网站推广
  • 衡水哪儿专业做网站百度可以发布广告吗
  • 网站建设 风险防控网站建设方案书
  • 丝芙兰网站做的好差前端seo优化
  • 深圳网站建站费用郑州seo优化培训