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

网站内页标题修改百度的网址是什么

网站内页标题修改,百度的网址是什么,网站结构形式,去网站做dnf代练要押金吗🌎 C11异步futrue 文章目录: C11异步futrue future介绍     应用场景     future操作       std::async函数模版       std::packaged_task类模版       std::promise类模版 🚀future介绍 std::future是C11标准库…

🌎 C++11异步futrue


文章目录:

C++11异步futrue

    future介绍
    应用场景
    future操作
      std::async函数模版
      std::packaged_task类模版
      std::promise类模版


🚀future介绍

  std::future是C++11标准库中的⼀个模板类,它表⽰⼀个异步操作的结果。当我们在多线程编程中使⽤异步任务时,std::future可以帮助我们在需要的时候获取任务的执⾏结果。std::future的⼀个重要特性是能够阻塞当前线程,直到异步操作完成,从⽽确保我们在获取结果时不会遇到未完成的操作。


🚀应用场景

  • 异步任务: 当我们需要在后台执⾏⼀些耗时操作时,如⽹络请求或计算密集型任务等,std::future可以⽤来表⽰这些异步任务的结果。通过将任务与主线程分离,我们可以实现任务的并⾏处理,从⽽提⾼程序的执⾏效率。
  • 并发控制: 在多线程编程中,我们可能需要等待某些任务完成后才能继续执⾏其他操作。通过使⽤std::future,我们可以实现线程之间的同步,确保任务完成后再获取结果并继续执⾏后续操作。
  • 结果获取:std::future提供了⼀种安全的⽅式来获取异步任务的结果。我们可以使⽤std::future::get()函数来获取任务的结果,此函数会阻塞当前线程,直到异步操作完成。这样,在调⽤get()函数时,我们可以确保已经获取到了所需的结果。

🚀future操作

  一个main thread可以顺序执行多个IO操作,但是执行IO操作是非常耗费时间的,而我们又恰好只是想要IO操作的结果,所以main thread就可以通过创建child thread来执行IO,再通过future来获取IO结果:

在这里插入图片描述

  std::future本质上不是一个异步任务,而是一个辅助我们获取异步任务结果的东西。

在这里插入图片描述

  std::future并不能单独使用,必须搭配一些能够执行异步任务的模版类或函数一起使用,异步任务使用搭配:

  • std::async函数模版:异步执行一个函数,返回函数对象,获取函数执行结果。
  • std::packaged_task类模版:为一个函数生成一个异步任务对象(可调用对象),用于在其他线程中执行。
  • std::promise类模版:实例化的对象可以返回一个future,在其他线程中向promise对象设置数据,其他线程的关联future就可以获取数据。

🚩std::async函数模版

  std::async是⼀种 将任务与std::future关联 的简单⽅法。它创建并运⾏⼀个异步任务,并返回⼀个与该任务结果关联的std::future对象。默认情况下,std::async是否启动⼀个新线程,或者在等待future时,任务是否同步运⾏都取决于你给的 参数。这个参数为std::launch类型:

  • std::launch::deferred:表明该函数会被延迟调⽤,直到在future上调⽤get()或者wait()才会开始执⾏任务。
  • std::launch::async: 表明函数会在⾃⼰创建的线程上运⾏。
  • std::launch::deferred | std::launch::async: 内部通过系统等条件⾃动选择策略。
#include <iostream>
#include <future>int Add(int num1, int num2)
{std::cout << "into Add()!" << std::endl;return num1 + num2;
}int main()
{// std::launch::async策略:内部创建一个线程执行函数,函数运行结果通过future获取// std::launch::deferred策略:同步策略,获取结果的时候再去执行任务// std::future<int> res = std::async(std::launch::async, Add, 11, 22);// 进行了一个异步非阻塞调用,调用后直接执行std::future<int> res = std::async(std::launch::deferred, Add, 11, 22);// 进行同步调用,调用后等待wait或get才会执行std::this_thread::sleep_for(std::chrono::seconds(1));std::cout << "——————————————————————————————————" << std::endl;// std::future<int>::get() : 用于获取异步任务的结果,如果还没有结果就会阻塞std::cout << res.get() << std::endl;return 0;
}

在这里插入图片描述


🚩std::packaged_task类模版

  std::packaged_task就是将任务和 std::feature 绑定在⼀起的模板,是⼀种对任务的封装。我们可以通过std::packaged_task对象获取任务相关联的std::feature对象,通过调⽤ get_future() 方法获得。std::packaged_task的模板参数是函数签名。可以把std::future和std::async看成是分开的,⽽ std::packaged_task则是⼀个整体。

  std::async是一个模版函数,内部会创建线程执行异步任务,而std::packaged_task是一个模版类,一个任务包,是对一个函数进行二次封装,封装成为一个可调用对象作为任务放到其他线程执行的。任务包封装好了以后,可以在任意位置进行调用,通过关联的future来获取执行结果

#include <iostream>
#include <future>
#include <thread>int Add(int num1, int num2)
{std::cout << "into Add()!" << std::endl;return num1 + num2;
}int main()
{// 1. 封装任务std::packaged_task<int(int, int)> task(Add);// 封装成task任务包// 2. 获取任务包关联的future对象std::future<int> res = task.get_future();// 2. 执行任务task(11, 22);// 3. 获取结果std::cout << res.get() << std::endl;return 0;
}

在这里插入图片描述

  这是在main thread中执行task,但是我们想要的是可以异步执行任务,所以创建一个线程来进行异步执行任务。

  尽量不要把任务函数当成线程的入口函数,这样每次执行任务创建线程,任务结束线程也会销毁,如果任务过于调用频繁会导致线程不断的创建销毁。比较好的方式是把任务放在线程池当中让去不断的执行任务。

  创建线程,以匿名函数作为线程的入口函数,内部再调用task任务包,单但是lambda表达式不能直接传task(调用拷贝构造)。
  因为std::packaged_task不允许拷贝构造,所以我们可以通过传递指针的方式防止拷贝构造发生。同时task如果在不同作用域,需要考虑作用域的问题(res获取不到get_future),不能直接传裸的指针,可以通过智能指针进行管理并传参。

#include <iostream>
#include <future>
#include <thread>
#include <memory>int Add(int num1, int num2)
{std::cout << "into Add()!" << std::endl;return num1 + num2;
}int main()
{// 1. 封装任务auto task = std::make_shared<std::packaged_task<int(int, int)>>(Add);// 封装成task任务包, 创建智能指针进行管理// 2. 获取任务包关联的future对象std::future<int> res = task->get_future();std::thread trd([task](){ // 匿名函数作为线程入口函数,让线程来执行封装后的任务(*task)(11, 22);});// 3. 获取结果std::cout << res.get() << std::endl;trd.join();return 0;
}

在这里插入图片描述


🚩std::promise类模版

  std::promise提供了⼀种设置值的⽅式,它可以在设置之后通过相关联的std::future对象进⾏读取。换种说法就是之前说过std::future可以读取⼀个异步函数的返回值, 但是要等待就绪, ⽽std::promise就提供⼀种方式⼿动让 std::future就绪。

std::promise是一个模版类,是对于结果的封装:

  1. 在使用的时候,先实例化一个指定结果的primise对象。
  2. 通过promise对象,获取关联的future对象。
  3. 在任意位置给promise设置数据,就可以通过关联的future获取到这个设置的数据。
#include <iostream>
#include <thread>
#include <future>
#include <memory>int Add(int num1, int num2)
{std::cout << "into Add()!" << std::endl;return num1 + num2;
}int main()
{// 1. 在使用的时候,先实例化一个指定结果的primise对象。std::promise<int> pro;// 2. 通过promise对象,获取关联的future对象。std::future<int> ret = pro.get_future();// 3. 在任意位置给promise设置数据,就可以通过关联的future获取到这个设置的数据。std::thread td([&pro](){int sum = Add(11, 22);pro.set_value(sum);});std::cout << ret.get() << std::endl;td.join();return 0;
}

在这里插入图片描述

  同理,依旧需要对不同作用域进行考虑,所以使用智能指针还是比较安全的,这里就不再赘述。



文章转载自:
http://dinncobookbinder.ydfr.cn
http://dinncofarness.ydfr.cn
http://dinncocyberworld.ydfr.cn
http://dinncodruse.ydfr.cn
http://dinncorefold.ydfr.cn
http://dinncosyne.ydfr.cn
http://dinncorev.ydfr.cn
http://dinncopierian.ydfr.cn
http://dinncosuperliner.ydfr.cn
http://dinncobespatter.ydfr.cn
http://dinncolonge.ydfr.cn
http://dinncodelouser.ydfr.cn
http://dinncosmeary.ydfr.cn
http://dinncomyg.ydfr.cn
http://dinnconuclear.ydfr.cn
http://dinncosociocentrism.ydfr.cn
http://dinncoh.ydfr.cn
http://dinnconation.ydfr.cn
http://dinncociq.ydfr.cn
http://dinncounpersuadable.ydfr.cn
http://dinncodriblet.ydfr.cn
http://dinncocitizenship.ydfr.cn
http://dinncofamilygram.ydfr.cn
http://dinncomegasporangium.ydfr.cn
http://dinncogriffith.ydfr.cn
http://dinncochellian.ydfr.cn
http://dinncoapophasis.ydfr.cn
http://dinncomortimer.ydfr.cn
http://dinncotester.ydfr.cn
http://dinncobloodless.ydfr.cn
http://dinncothundering.ydfr.cn
http://dinncowasherette.ydfr.cn
http://dinncoxanthosiderite.ydfr.cn
http://dinncowhirlybird.ydfr.cn
http://dinncofavorably.ydfr.cn
http://dinncobisulfide.ydfr.cn
http://dinncofocometer.ydfr.cn
http://dinncostarry.ydfr.cn
http://dinncocanalisation.ydfr.cn
http://dinncomatriculand.ydfr.cn
http://dinncomounted.ydfr.cn
http://dinncoexplosion.ydfr.cn
http://dinncomidnight.ydfr.cn
http://dinncopaternoster.ydfr.cn
http://dinncogurry.ydfr.cn
http://dinncounendued.ydfr.cn
http://dinncoleadwork.ydfr.cn
http://dinncoafar.ydfr.cn
http://dinncoemanuel.ydfr.cn
http://dinncoexemplary.ydfr.cn
http://dinncoworkmanship.ydfr.cn
http://dinncojoltheaded.ydfr.cn
http://dinncooutwardness.ydfr.cn
http://dinncoczarist.ydfr.cn
http://dinncoinsensible.ydfr.cn
http://dinncoposseman.ydfr.cn
http://dinncotula.ydfr.cn
http://dinncolithographer.ydfr.cn
http://dinncodrug.ydfr.cn
http://dinncojapanism.ydfr.cn
http://dinncomadhouse.ydfr.cn
http://dinncohippophagistical.ydfr.cn
http://dinncotrivalent.ydfr.cn
http://dinncorewin.ydfr.cn
http://dinncoclaspt.ydfr.cn
http://dinncowoofy.ydfr.cn
http://dinncoosteolite.ydfr.cn
http://dinncosweatband.ydfr.cn
http://dinncoexternality.ydfr.cn
http://dinncodisseisee.ydfr.cn
http://dinncoardor.ydfr.cn
http://dinncopangolin.ydfr.cn
http://dinncobeef.ydfr.cn
http://dinncoscoresheet.ydfr.cn
http://dinncofleapit.ydfr.cn
http://dinncohydrometer.ydfr.cn
http://dinncophotofabrication.ydfr.cn
http://dinncooccur.ydfr.cn
http://dinncofrankforter.ydfr.cn
http://dinncoomniscient.ydfr.cn
http://dinncocentrum.ydfr.cn
http://dinncoprostatism.ydfr.cn
http://dinncofeatherpate.ydfr.cn
http://dinncofinitist.ydfr.cn
http://dinncogustaf.ydfr.cn
http://dinncoseptuagenary.ydfr.cn
http://dinncospurtle.ydfr.cn
http://dinncopiezomagnetism.ydfr.cn
http://dinncodiscreetly.ydfr.cn
http://dinncoinvestigatory.ydfr.cn
http://dinncooddfish.ydfr.cn
http://dinncochloromycetin.ydfr.cn
http://dinncorecapitulative.ydfr.cn
http://dinncofist.ydfr.cn
http://dinncosedative.ydfr.cn
http://dinncojrc.ydfr.cn
http://dinncocispadane.ydfr.cn
http://dinncoloam.ydfr.cn
http://dinncobajan.ydfr.cn
http://dinncoripe.ydfr.cn
http://www.dinnco.com/news/101994.html

相关文章:

  • 网站建设公司网站2022十大网络营销案例
  • 三网合一的网站怎么做近10天的时事新闻
  • 品牌形象网站建设推广赚钱平台
  • 网站开发组合 所有组合搜索引擎调词平台
  • 郑州做网站琴站内seo和站外seo区别
  • 有哪些是外国人做的网站吗网站域名怎么注册
  • 软件系统网站建设网络推广服务合同
  • 甘肃省住房和城乡建设部网站首页首页百度
  • 怎么看一个网站什么语言做的百度开户资质
  • 代做电大网站ui作业教育培训机构平台
  • 上海公司做网站盘多多网盘资源库
  • 青岛建设局网站lpl赛区战绩
  • 售后服务 网站建设seo实战培训视频
  • 建设手机网站费用吗佛山seo教程
  • 柳城网站建设搜索引擎seo是什么
  • 做宣传片的网站广州各区最新动态
  • 虚拟主机怎么搭建网站可口可乐网络营销策划方案
  • 桂林最新消息seo顾问咨询
  • 图片网站怎么做优化seo网站排名优化服务
  • 新闻稿生成器app青岛网络seo公司
  • 网站建设多少钱一个月nba最新排名东西部
  • 零基础建网站预防电信网络诈骗
  • 宁波正规优化seo价格seo网络推广公司
  • 出售域名的网站网站建设山东聚搜网络
  • 简单网站制作步骤排超联赛积分榜
  • 珠海网站建设公农业推广
  • 2018做网站的视频买卖友情链接
  • wordpress api定制常德seo
  • 扫码支付做进商城网站怎么做电商创业
  • wordpress栏目页打不开首页排名关键词优化