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

不需要证件做网站微信scrm系统

不需要证件做网站,微信scrm系统,建设网站 费用,代做底单的网站目录 1.概述 2.std::future的基本用法 3.使用 std::shared_future 4.std::future的使用场景 5.总结 1.概述 在编程实践中,我们常常需要使用异步调用。通过异步调用,我们可以将一些耗时、阻塞的任务交给其他线程来执行,从而保证当前线程的…

目录

1.概述

2.std::future的基本用法

3.使用 std::shared_future

4.std::future的使用场景

5.总结


1.概述

        在编程实践中,我们常常需要使用异步调用。通过异步调用,我们可以将一些耗时、阻塞的任务交给其他线程来执行,从而保证当前线程的快速响应能力。还有一些场景可以通过将一个任务,分成多个部分然后将这部分交给多个线程来进行并发执行,从而完成任务的快速计算执行,提高应用性能。这里就需要用到异步调用的概念。

        异步调用,就是当前线程将一个任务交给另外一个线程来进行执行,当前线程会接着执行当前任务,不需要等待这个交付给其他线程执行的任务的结果,直到其在未来的某一个时刻需要使用这个任务执行结果数据的时候。

        std::future 是 C++11 标准库中引入的一个非常重要的特性,它提供了一种机制来访问异步操作的结果。当你启动一个异步操作(比如,通过 std::asyncstd::packaged_taskstd::promise 等)时,你可以立即获得一个 std::future 对象,这个对象将在异步操作完成时持有操作的结果。

2.std::future的基本用法

要理解 std::future 的用法,我们需要先了解几个相关的组件:

  • std::async:用于启动异步任务,并返回一个 std::future 对象。

  • std::promise:用于设置将来某个时刻的值,可以与 std::future 配合使用。

  • std::future:用于获取异步操作的结果。

1)使用 std::async 创建异步任务

std::async 是最简单、最直接的创建异步任务的方式。我们来看一个简单的例子:

#include <iostream>
#include <future>
#include <chrono>int main() {// 启动一个异步任务,计算某个复杂的结果std::future<int> result = std::async(std::launch::async, []() -> int {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作return 42;});std::cout << "Doing something else while waiting for the result..." << std::endl;// 等待并获取结果int value = result.get();std::cout << "Result: " << value << std::endl;return 0;
}

        在这个例子中,我们使用 std::async 启动了一个异步任务,该任务在后台计算一个结果并返回一个 std::future 对象。我们可以继续执行其他操作,直到需要获取异步任务的结果时,调用 result.get() 阻塞等待并获取结果。

2)使用 std::promise 和 std::future

        除了 std::async,我们还可以使用 std::promise 和 std::future 来实现更灵活的异步任务管理。std::promise 用于在某个时刻设置结果,而 std::future 用于在稍后获取该结果。来看一个具体的例子:

#include <iostream>
#include <future>
#include <thread>void compute(std::promise<int>& prom) {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作prom.set_value(42); // 设置结果
}int main() {std::promise<int> prom;std::future<int> fut = prom.get_future();std::thread t(compute, std::ref(prom));std::cout << "Doing something else while waiting for the result..." << std::endl;int value = fut.get();std::cout << "Result: " << value << std::endl;t.join();return 0;
}

        在这个例子中,我们创建了一个 std::promise 对象,并通过 get_future() 获取与之关联的 std::future 对象。然后,我们启动一个线程,在该线程中执行耗时操作并设置结果。主线程可以继续执行其他操作,直到调用 fut.get() 阻塞等待并获取结果。

3)std::future的尝试等待

std::future 还提供了 wait_for() 和 wait_until() 方法,允许你尝试等待一段时间或直到某个时间点。这些方法不会永久阻塞线程,而是会在指定的时间或条件到达时返回。如:

#include <iostream>
#include <vector>
#include <future>
#include <thread>
#include <chrono>int async_task(int id) {std::this_thread::sleep_for(std::chrono::seconds(id));return id * id;
}int main() {std::vector<std::future<int>> futures;for (int i = 1; i <= 3; ++i) {futures.push_back(std::async(std::launch::async, async_task, i));}for (auto& fut : futures) {std::cout << "Result: " << fut.get() << std::endl;}return 0;
}

4)std::future的错误处理

        如果异步操作中抛出了异常,并且该异常没有被捕获,那么当你调用 get() 方法时,这个异常会被重新抛出。因此,你需要准备好处理这些可能的异常。如:

#include <iostream>
#include <future>
#include <thread>void compute(std::promise<int>& prom) {try {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作throw std::runtime_error("Something went wrong"); // 抛出异常prom.set_value(42); // 设置结果} catch (...) {prom.set_exception(std::current_exception()); // 设置异常}
}int main() {std::promise<int> prom;std::future<int> fut = prom.get_future();std::thread t(compute, std::ref(prom));std::cout << "Doing something else while waiting for the result..." << std::endl;try {int value = fut.get();std::cout << "Result: " << value << std::endl;} catch (const std::exception& e) {std::cerr << "Caught exception: " << e.what() << std::endl;}t.join();return 0;
}

        在这个例子中,我们在 compute 函数中抛出了一个异常,并在 std::promise 对象中设置该异常。主线程在调用 fut.get() 时,会捕获并处理这个异常。 

注意事项

  • 每个 std::future 对象只能调用一次 get() 方法。
  • 一旦 std::future 对象的 get() 方法被调用并返回了结果,或者 std::future 对象被移动,它就不能再用来获取结果了。
  • 如果异步操作抛出异常且该异常在 std::future 对象被销毁前未被捕获,则程序将终止。因此,请确保在销毁 std::future 对象之前调用 get() 方法并适当处理异常。

3.使用 std::shared_future

        有时候,我们希望多个线程可以共享一个 std::future 对象的结果。C++11 引入了 std::shared_future,它允许多个线程安全地访问同一个异步操作的结果。来看一个例子:

#include <iostream>
#include <future>
#include <thread>
#include <vector>void compute(std::shared_future<int> fut) {std::cout << "Thread got result: " << fut.get() << std::endl;
}int main() {std::promise<int> prom;std::shared_future<int> fut = prom.get_future().share();std::vector<std::thread> threads;for (int i = 0; i < 3; ++i) {threads.emplace_back(compute, fut);}std::this_thread::sleep_for(std::chrono::seconds(2));prom.set_value(42);for (auto& t : threads) {t.join();}return 0;
}

        在这个例子中,我们使用 std::promise 创建了一个 std::shared_future 对象,并将其传递给多个线程。这些线程可以共享并安全地访问同一个异步操作的结果。

4.std::future的使用场景

  • 并行计算:当你需要将计算任务分配给多个线程并行执行,并需要等待所有任务完成时。
  • 异步IO:在进行耗时的IO操作时,可以使用异步方式执行,并在操作完成时继续处理。
  • 性能优化:通过异步处理非关键路径上的操作,可以提高应用程序的响应性和吞吐量。

5.总结

   std::future 是 C++ 中进行异步编程的重要工具,它简化了异步操作的启动、结果查询和异常处理。通过合理使用 std::future 和相关机制,可以编写出既高效又易于维护的异步代码。

http://www.hkea.cn/news/230029/

相关文章:

  • 如何申请一个网站 做视频百度小说搜索热度排行榜
  • 天津做网站选择津坤科技b重庆seo教程搜索引擎优化
  • 什么网站做热能表好百度一下电脑版首页网址
  • 点击图片直接进入网站怎么做如何使用免费b站推广网站
  • 手机网站建设软件怎么在百度上做广告推广
  • 南京做网站团队手机app免费制作平台
  • 17173游戏网搜索优化指的是什么
  • 公司做网站需要给百度交钱吗百度竞价推广方案
  • 网站建设的关键seo推广小分享
  • 写小说的小网站百度关键词排名优化
  • 制作网站的成本规划公司如何建立网站
  • html语言做网站石嘴山网站seo
  • 做最好的言情网站官网seo优化
  • 云南建设监理协会网站营销失败案例分析
  • 怎么样做淘宝优惠券网站搜索引擎营销的优缺点
  • wordpress动态订单seo社区
  • 网站域没到期不能续费吗google谷歌搜索
  • 厦门好的做网站公司网络营销推广方式都有哪些
  • 重庆市建设工程信息官网站自己做网站的流程
  • 网站建设公司怎么做网络营销网站推广
  • 360应用商店seo服务套餐
  • 废橡胶网站建设个人博客网页设计
  • 什么网站做一手项目好域名查询官网
  • 做日用品的要找什么网站好站长工具端口检测
  • 贵州软件开发 网站开发手机版百度一下
  • 企业网站建立答辩问题百度怎么发布广告
  • 温州快建网站地推拉新接单网
  • 濉溪县城乡建设委员会燃气办网站热狗网站排名优化外包
  • 网站能不能自己做免费的seo教程
  • 湖南的商城网站建设优化教程网下载