c++ std::async怎么用 c++异步任务入门【教程】

std::async是C++11引入的异步任务启动方式,通过std::future获取结果;需明确指定std::launch::async策略以确保真正并发,避免默认混合策略导致意外同步;get()、wait()及带超时的wait_for/ wait_until用于结果获取与等待。

std::async 是 C++11 引入的轻量级异步任务启动方式,它帮你自动创建线程、管理执行,并通过 std::future 获取结果。用对了不复杂,但几个关键点没注意就容易卡死或行为异常。

基础用法:启动一个异步函数

最常见写法是传入可调用对象(函数、lambda、绑定表达式)和参数,返回一个 std::future:

#include 
#include 

int compute(int x) { return x x + 2 x + 1; }

int main() { // 启动异步计算,不阻塞当前线程 auto fut = std::async(std::launch::async, compute, 5);

// 做其他事……
std::cout << "doing something else...\n";

// 获取结果(这里会等待完成)
int result = fut.get(); // 输出 36
std::cout << "result: " << result << "\n";

}

注意:std::launch::async 明确要求新线程执行;如果不写这个策略,默认是 std::launch::async | std::launch::deferred,意味着可能延迟执行(调用 get/wait 时才同步运行),这点常被忽略。

两种启动策略的区别

  • std::launch::async:一定新开线程,真正并行执行
  • std::launch::deferred:不立即执行,get() 或 wait() 时才在当前线程同步调用(类似普通函数调用)

混合策略(默认)会让系统自由选择,但可能导致意外同步行为。建议明确指定策略,尤其想确保并发时,直接写 std::launch::async

获取结果的几种方式

std::future 提供三种等待/取值方法:

  • fut.get():获取结果,同时隐式等待完成;只能调用一次,之后 future 失效
  • fut.wait():只等待,不取值,适合“等它做完再干别的”
  • fut.wait_for(...)fut.wait_until(...):带超时的等待,返回 future_status 枚举(ready / timeout / deferred)

例如检查是否超时:

auto status = fut.wait_for(std::chrono::seconds(2));
if (status == std::future_status::ready) {
    std::cout << "done: " << fut.get() << "\n";
} else {
    std::cout << "timeout or still running\n";
}

捕获异常也很重要

异步函数里抛出的异常不会直接冒泡到主线程,而是被捕获并存储在 future 中。调用 get() 时才会重新抛出:

auto fut = std::async([]{
    throw std::runtime_error("oops in async");
});

try { fut.get(); // 这里才真正抛出异常 } catch (const std::exception& e) { std::cout << "caught: " << e.what() << "\n"; }

所以别忘了 try-catch,否则未处理异常会导致程序终止。