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

网站开发与维护是干什么的做网站虚拟服务器

网站开发与维护是干什么的,做网站虚拟服务器,打开网站速度,金坛做网站哪家好C 标准库中的 future 和 async 提供了一种便捷的方式来实现并发编程。std::async 是一个非常强大的工具#xff0c;它可以用于启动异步任务#xff0c;并返回一个 std::future 对象#xff0c;该对象可以用来等待任务的结果。 std::async 的基本用法 std::async 用于启动一…C 标准库中的 future 和 async 提供了一种便捷的方式来实现并发编程。std::async 是一个非常强大的工具它可以用于启动异步任务并返回一个 std::future 对象该对象可以用来等待任务的结果。 std::async 的基本用法 std::async 用于启动一个异步任务。它的基本语法如下 #include iostream #include future #include thread// 一个简单的函数模拟一些工作 int work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟延迟return x * x; }int main() {// 使用 std::async 启动异步任务并获取 std::future 对象std::futureint result std::async(std::launch::async, work, 10);// 在此处可以执行其他工作std::cout Doing other work... std::endl;// 获取异步任务的结果会阻塞直到结果准备好int value result.get();std::cout Result: value std::endl;return 0; }std::async 启动模式 std::async 提供了两种启动模式 std::launch::async任务在独立线程中异步执行。std::launch::deferred任务延迟执行直到 future 对象的 get 或 wait 方法被调用。 示例不同的启动模式 #include iostream #include future #include threadint work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2));return x * x; }int main() {// 异步启动std::futureint async_result std::async(std::launch::async, work, 10);// 延迟启动std::futureint deferred_result std::async(std::launch::deferred, work, 20);// 在此处可以执行其他工作std::cout Doing other work... std::endl;// 获取异步任务的结果会阻塞直到结果准备好int async_value async_result.get();std::cout Async result: async_value std::endl;// 获取延迟任务的结果会在此时执行任务int deferred_value deferred_result.get();std::cout Deferred result: deferred_value std::endl;return 0; }使用 std::future 获取结果 std::future 提供了多种方法来获取任务结果 get阻塞当前线程直到任务完成并返回结果。wait阻塞当前线程直到任务完成。wait_for阻塞当前线程一段时间等待任务完成。wait_until阻塞当前线程直到指定时间等待任务完成。 示例使用 std::future 的不同方法 #include iostream #include future #include threadint work(int x) {std::this_thread::sleep_for(std::chrono::seconds(2));return x * x; }int main() {std::futureint result std::async(std::launch::async, work, 10);// 等待一段时间if (result.wait_for(std::chrono::seconds(1)) std::future_status::timeout) {std::cout Task is still running... std::endl;}// 再次等待直到任务完成result.wait();int value result.get();std::cout Result: value std::endl;return 0; }捕获异常 如果异步任务在执行过程中抛出异常std::future 的 get 方法会重新抛出该异常。可以通过捕获异常来处理任务中的错误。 示例捕获异常 #include iostream #include future #include stdexcept #include threadint work(int x) {if (x 0) {throw std::invalid_argument(x must be non-negative);}std::this_thread::sleep_for(std::chrono::seconds(2));return x * x; }int main() {// 启动一个会抛出异常的任务std::futureint result std::async(std::launch::async, work, -10);try {int value result.get(); // 这里会重新抛出异常std::cout Result: value std::endl;} catch (const std::exception e) {std::cout Exception: e.what() std::endl;}return 0; }总结 std::async用于启动异步任务可以指定启动模式async 或 deferred。std::future用于获取异步任务的结果提供多种方法get, wait, wait_for, wait_until。异常处理通过 std::future 的 get 方法可以捕获异步任务中抛出的异常。
http://www.pierceye.com/news/172463/

相关文章:

  • 门户网站建站流程做网站在哪里做比较好
  • 青创网站首页wordpress用户发文章
  • wordpress 仿站 主题网站建设拍金手指排名贰拾
  • 自己的网站怎么做跳转广州白云建方舱医院
  • 免费搭建购物网站网页游戏网站打不开
  • 专业的东莞网站设计wordpress extra script
  • 嘉兴网站开发公司电话从零开始创建wordpress主题.pdf
  • 备案号怎么添加到网站自己做网站原始代码
  • 可以做exe的网站邯郸做紧固件网站
  • 电子商务网站开发的说法移动端首页尺寸
  • 普通网站服务器房地产营销门户网站开发
  • 免费做公司网站wordpress文章格式
  • 制作网站教学设计金湖县城乡建设局网站
  • 微商城网站建设咨询网站做的不满意
  • 装企工长网站开发互联网营销师考试内容
  • 广州高端网站定制公司哪家好全椒县城乡规划建设局网站
  • 物流网站毕业设计论文论坛网站模板下载
  • 代理上网蜗牛精灵seo
  • 网站怎么做用qq登录接入网络舆情监测分析系统
  • 怎么把几个网站做互通wordpress 英文站
  • 建设高端网站需要多少钱wordpress检测
  • 如何学习网站建设app嘉兴建站软件
  • 驻马店市建设工程网站平面设计培训班学费一般多少百度贴吧
  • 长沙网络建设的网站免费的个人简历模板word下载
  • 网站维护简单吗wordpress绿色两栏响应式主题
  • 二手站网站怎做福州网站建设的公司哪家好
  • dw如何做网站后台佛山行业网站建设
  • 如何做网站轮播大图简单网页制作成品代码
  • 网站怎么做uc整合查企业网站
  • 网站没被收录什么原因网站排名点击工具