std::async和std::future用于异步任务执行与结果获取,前者启动任务并返回future对象,后者通过get()获取结果或异常,支持指定启动策略,并可用于并行处理多个任务。

在C++11中,std::async 和 std::future 提供了一种简单的方式来启动异步任务并获取其结果。它们位于 red"><future> 头文件中,是处理并发编程的重要工具。
基本概念
std::async 用于启动一个异步任务,可以看作是一个“自动包装的线程”,它会在后台执行某个函数。
std::future 是一个占位符对象,用来在未来某个时间点获取异步操作的结果。
当你调用 std::async,它会返回一个 std::future 对象,你可以通过调用该对象的 get() 方法来获取结果。如果结果还没准备好,get() 会阻塞直到结果可用。
使用示例:基本异步计算
下面是一个简单的例子,演示如何使用 std::async 计算两个大数的和,并通过 std::future 获取结果:
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <future>
#include <thread>
<p>int heavy_computation(int a, int b) {
std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟耗时操作
return a + b;
}</p><p>int main() {
// 启动异步任务
std::future<int> future_result = std::async(heavy_computation, 100, 200);</p><pre class='brush:php;toolbar:false;'>std::cout << "正在执行其他操作...\n";
// 获取结果(等待完成)
int result = future_result.get();
std::cout << "计算结果: " << result << "\n";
return 0;}
指定启动策略
std::async 支持两种启动策略:
- std::launch::async:强制异步运行(创建新线程)
- std::launch::deferred:延迟运行,直到调用 get() 或 wait() 时才在当前线程执行
默认情况下,std::async 使用 std::launch::async | std::launch::deferred,表示由系统决定。
如果你想确保任务在新线程中运行:
std::future<int> future_result = std::async(
std::launch::async,
heavy_computation, 100, 200
);
如果你希望延迟执行(用于优化或调试):
std::future<int> future_result = std::async(
std::launch::deferred,
heavy_computation, 100, 200
);
多个异步任务并行处理
你也可以同时启动多个异步任务,然后分别获取结果:
#include <iostream>
#include <future>
#include <vector>
<p>int square(int x) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
return x * x;
}</p><p>int main() {
std::vector<std::future<int>> futures;</p><pre class='brush:php;toolbar:false;'>for (int i = 1; i <= 5; ++i) {
futures.push_back(std::async(square, i));
}
for (auto& fut : futures) {
std::cout << fut.get() << " ";
}
std::cout << "\n";
return 0;}
输出:
1 4 9 16 25
异常处理
如果异步任务抛出异常,这个异常会被捕获并存储。当你调用 future 的 get() 时,异常会重新抛出。
std::future<void> f = std::async([]{
throw std::runtime_error("出错了!");
});
<p>try {
f.get();
} catch (const std::exception& e) {
std::cout << "捕获异常: " << e.what() << "\n";
}</p>注意事项
- 每个 future 只能调用一次 get(),多次调用会导致未定义行为。
- 如果不调用 get() 或 wait(),某些实现可能会阻塞在析构函数中(特别是使用 deferred 策略时)。
- std::async 的资源管理依赖于 future 的生命周期,不要提前销毁 future。
基本上就这些。std::async 和 std::future 非常适合用于简单的异步任务,不需要手动管理线程。对于更复杂的场景(如线程池),可能需要结合 std::promise、std::packaged_task 或直接使用 std::thread。











