海口网站建设哪家好,wordpress主题带个人中心,拓者设计吧电脑版,西安seo网站关键词目录
1.概述
2.std::future的基本用法
3.使用 std::shared_future
4.std::future的使用场景
5.总结 1.概述 在编程实践中#xff0c;我们常常需要使用异步调用。通过异步调用#xff0c;我们可以将一些耗时、阻塞的任务交给其他线程来执行#xff0c;从而保证当前线程的…目录
1.概述
2.std::future的基本用法
3.使用 std::shared_future
4.std::future的使用场景
5.总结 1.概述 在编程实践中我们常常需要使用异步调用。通过异步调用我们可以将一些耗时、阻塞的任务交给其他线程来执行从而保证当前线程的快速响应能力。还有一些场景可以通过将一个任务分成多个部分然后将这部分交给多个线程来进行并发执行从而完成任务的快速计算执行提高应用性能。这里就需要用到异步调用的概念。 异步调用就是当前线程将一个任务交给另外一个线程来进行执行当前线程会接着执行当前任务不需要等待这个交付给其他线程执行的任务的结果直到其在未来的某一个时刻需要使用这个任务执行结果数据的时候。 std::future 是 C11 标准库中引入的一个非常重要的特性它提供了一种机制来访问异步操作的结果。当你启动一个异步操作比如通过 std::async、std::packaged_task、std::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 chronoint main() {// 启动一个异步任务计算某个复杂的结果std::futureint 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 threadvoid compute(std::promiseint prom) {std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作prom.set_value(42); // 设置结果
}int main() {std::promiseint prom;std::futureint 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() 阻塞等待并获取结果。
3std::future的尝试等待
std::future 还提供了 wait_for() 和 wait_until() 方法允许你尝试等待一段时间或直到某个时间点。这些方法不会永久阻塞线程而是会在指定的时间或条件到达时返回。如
#include iostream
#include vector
#include future
#include thread
#include chronoint async_task(int id) {std::this_thread::sleep_for(std::chrono::seconds(id));return id * id;
}int main() {std::vectorstd::futureint 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;
}
4std::future的错误处理 如果异步操作中抛出了异常并且该异常没有被捕获那么当你调用 get() 方法时这个异常会被重新抛出。因此你需要准备好处理这些可能的异常。如
#include iostream
#include future
#include threadvoid compute(std::promiseint 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::promiseint prom;std::futureint 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 对象的结果。C11 引入了 std::shared_future它允许多个线程安全地访问同一个异步操作的结果。来看一个例子
#include iostream
#include future
#include thread
#include vectorvoid compute(std::shared_futureint fut) {std::cout Thread got result: fut.get() std::endl;
}int main() {std::promiseint prom;std::shared_futureint fut prom.get_future().share();std::vectorstd::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 和相关机制可以编写出既高效又易于维护的异步代码。