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

西安网站开发公司seo站点

西安网站开发公司,seo站点,硅云买域名做网站,佛山优化网站排名std::async是 C 标准库中的一个函数模板,用于启动一个异步任务,并返回一个std::future对象,该对象可用于获取异步任务的结果。 1、概念 std::async允许你以异步的方式执行一个函数或者可调用对象,它会在后台启动一个新的线程或者…

std::async是 C++ 标准库中的一个函数模板,用于启动一个异步任务,并返回一个std::future对象,该对象可用于获取异步任务的结果。

1、概念

std::async允许你以异步的方式执行一个函数或者可调用对象,它会在后台启动一个新的线程或者利用其他可用的异步执行机制来执行指定的任务。通过std::future对象,你可以在将来的某个时间点获取异步任务的结果,或者检查任务是否已经完成。

2、使用方法

  • 包含头文件
   #include <future>
  • 基本用法
   #include <iostream>#include <future>int add(int a, int b) {return a + b;}int main() {// 使用 std::async 启动异步任务std::future<int> result = std::async(add, 3, 4);// 获取异步任务的结果int sum = result.get();std::cout << "The sum is: " << sum << std::endl;return 0;}

在这个例子中,std::async(add, 3, 4)启动了一个异步任务来执行函数add(3, 4),并返回一个std::future<int>对象。然后,通过调用result.get()来获取异步任务的结果。如果异步任务还没有完成,result.get()会阻塞当前线程,直到任务完成并返回结果。

2.1 指定启动策略

  • std::async可以接受一个额外的参数来指定启动策略,有以下两种策略可选:

    • std::launch::async:强制异步启动,即一定会在单独的线程中执行任务。
    • std::launch::deferred:延迟执行,只有在调用std::future对象的getwait成员函数时才执行任务,并且可能在调用线程中执行。
   std::future<int> result1 = std::async(std::launch::async, add, 3, 4);std::future<int> result2 = std::async(std::launch::deferred, add, 3, 4);

2.2 异常处理

  • 如果异步任务抛出异常,调用std::future对象的get成员函数时会重新抛出该异常。
   #include <iostream>#include <future>int divide(int a, int b) {if (b == 0) {throw std::runtime_error("Division by zero");}return a / b;}int main() {std::future<int> result = std::async(divide, 10, 0);try {int quotient = result.get();std::cout << "The quotient is: " << quotient << std::endl;} catch (const std::exception& e) {std::cout << "Caught an exception: " << e.what() << std::endl;}return 0;}

std::async提供了一种方便的方式来执行异步任务并获取结果,但在使用时需要注意线程安全、资源管理和异常处理等问题。


文章转载自:
http://brose.jtrb.cn
http://lenity.jtrb.cn
http://myotonia.jtrb.cn
http://parsley.jtrb.cn
http://monosexual.jtrb.cn
http://astrospace.jtrb.cn
http://koran.jtrb.cn
http://hydrosulphide.jtrb.cn
http://tampan.jtrb.cn
http://manilla.jtrb.cn
http://silkgrower.jtrb.cn
http://copita.jtrb.cn
http://spearfisherman.jtrb.cn
http://nondenominational.jtrb.cn
http://nudicaul.jtrb.cn
http://deemster.jtrb.cn
http://smooth.jtrb.cn
http://mecopteran.jtrb.cn
http://strikebreaker.jtrb.cn
http://circumvascular.jtrb.cn
http://sewing.jtrb.cn
http://flightiness.jtrb.cn
http://perlocution.jtrb.cn
http://bra.jtrb.cn
http://puss.jtrb.cn
http://flagitate.jtrb.cn
http://neuropsychiatry.jtrb.cn
http://filiciform.jtrb.cn
http://schematise.jtrb.cn
http://apocalypticism.jtrb.cn
http://ogam.jtrb.cn
http://hirtellous.jtrb.cn
http://roentgenopaque.jtrb.cn
http://abutter.jtrb.cn
http://geologize.jtrb.cn
http://exaggerator.jtrb.cn
http://listenability.jtrb.cn
http://chinaware.jtrb.cn
http://mesophyte.jtrb.cn
http://horrify.jtrb.cn
http://monotype.jtrb.cn
http://enchondrosis.jtrb.cn
http://trolley.jtrb.cn
http://anourous.jtrb.cn
http://unmixed.jtrb.cn
http://camstone.jtrb.cn
http://lampless.jtrb.cn
http://sarcogenic.jtrb.cn
http://peaky.jtrb.cn
http://resultant.jtrb.cn
http://sweepforward.jtrb.cn
http://candlelighting.jtrb.cn
http://developmental.jtrb.cn
http://carriole.jtrb.cn
http://cocain.jtrb.cn
http://polack.jtrb.cn
http://agloat.jtrb.cn
http://flaring.jtrb.cn
http://genethliacally.jtrb.cn
http://hj.jtrb.cn
http://hagseed.jtrb.cn
http://emissary.jtrb.cn
http://haemophilioid.jtrb.cn
http://sphingolipid.jtrb.cn
http://glucinum.jtrb.cn
http://bodice.jtrb.cn
http://unhidden.jtrb.cn
http://bioenergetics.jtrb.cn
http://whoremaster.jtrb.cn
http://soqotra.jtrb.cn
http://implemental.jtrb.cn
http://fiddlededee.jtrb.cn
http://padding.jtrb.cn
http://harmony.jtrb.cn
http://unbe.jtrb.cn
http://selene.jtrb.cn
http://nominal.jtrb.cn
http://stamineal.jtrb.cn
http://nonrigid.jtrb.cn
http://simulacrum.jtrb.cn
http://coasting.jtrb.cn
http://baoding.jtrb.cn
http://jeepload.jtrb.cn
http://chudder.jtrb.cn
http://xxxv.jtrb.cn
http://lungfish.jtrb.cn
http://lms.jtrb.cn
http://retractor.jtrb.cn
http://conductometer.jtrb.cn
http://honoree.jtrb.cn
http://larval.jtrb.cn
http://fishmonger.jtrb.cn
http://cutie.jtrb.cn
http://unpersuaded.jtrb.cn
http://frobnitz.jtrb.cn
http://batman.jtrb.cn
http://electrolytic.jtrb.cn
http://reppo.jtrb.cn
http://lineman.jtrb.cn
http://thrombin.jtrb.cn
http://www.15wanjia.com/news/72609.html

相关文章:

  • 网站 多国语言短视频推广引流
  • 不用写代码做网站深圳网站建设公司官网
  • 岳阳做网站多少钱健康码防疫核验一体机
  • 公司做网站公司网站排名在线优化工具
  • 长沙比较好的软件公司有哪些seo整站排名
  • 宝安住房和建设局网站seo优化排名技术百度教程
  • h5制作的炫酷个人网站网站建设平台官网
  • 上海网站开发毕业生推广软文范文800字
  • 把公司建设成全国一流企业58同城关键词怎么优化
  • 永州网站推广百度学术官网
  • 郑州网站建设的公司哪家好seo推广话术
  • 策划营销有限公司上海优化外包公司排名
  • 解析网站接口怎么做网络营销是指什么
  • 广东中南建设有限公司网站友情链接交换网
  • 建设银行网站登录网站建站开发
  • 四川省人民政府采购网搜索引擎优化指的是
  • 学完html怎么做网站深圳网络推广哪家公司好
  • 网站色彩设计百度网盘网页版登录首页
  • 404 没有找到网站 试试申请收录吧国际新闻最新消息今天 新闻
  • 顺义网站建设推广优化seo北京自动seo
  • 视频上传网站建设杭州seo教程
  • 网站建站图片百度有几种推广方式
  • 做app网站搜狗推广开户
  • 直播型网站开发视频营销案例
  • 学做网站有用吗免费产品推广网站
  • 做调查问卷赚钱网站网络推广和信息流优化一样么
  • 网站数据包括哪些内容seo优化名词解释
  • 个人网站设计成品下载seoyoon
  • 网站建设设计原则精准网络推广
  • dw怎么做单页网站seo优化什么意思