C++如何使用future和promise_C++11中实现异步操作与线程间通信

std::future和std::promise通过共享状态实现线程间通信,允许异步任务返回结果或异常。使用std::async可快速创建异步任务并获取future;手动创建时,promise调用set_value或set_exception设置结果,future通过get获取值或抛出异常,支持移动语义跨线程传递所有权,结合wait_for、wait_until和valid可实现灵活的状态检查与同步控制。

在C++11中,std::futurestd::promise 是实现异步操作和线程间通信的重要工具。它们提供了一种机制,使得一个线程可以将结果传递给另一个线程,而无需共享变量或使用互斥锁进行复杂同步。

基本概念:future 与 promise 的作用

std::promise 是一个可写入一次的对象,用于设置某个值或异常;std::future 是与之关联的只读对象,用于获取这个值。两者通过共享状态连接,通常用于跨线程传递结果。

典型使用场景包括:

  • 从后台线程返回计算结果
  • 通知某事件已完成
  • 实现简单的线程协作

创建异步任务并获取结果

使用 std::async 可以快速启动异步任务,并自动返回一个 std::future

#include 
#include 

int compute() {
    return 42;
}

int main() {
    std::future fut = std::async(compute);
    int result = fut.get(); // 阻塞直到结果可用
    std::cout << "Result: " << result << "\n";
    return 0;
}

这里 fut.get() 会等待任务完成并取出结果。如果任务抛出异常,调用 get 会重新抛出该异常。

手动使用 promise 实现线程通信

当需要更精细控制时,可以手动绑定 promisefuture

#include 
#include 
#include 

void set_value(std::promise&& prom) {
    prom.set_value(100); // 设置结果
}

int main() {
    std::promise prom;
    std::future fut = prom.get_future();

    std::thread t(set_value, std::move(prom));
    
    int value = fut.get();
    std::cout << "Received: " << value << "\n";

    t.join();
    return 0;
}

注意:每个 promise 只能调用一次 set_value,否则会抛出异常。移动语义用于在线程间转移所有权。

处理异常与状态检查

除了正常值,promise 还可以传递异常:

void may_throw(std::promise&& p) {
    try {
        throw std::runtime_error("Something went wrong");
    } catch (...) {
        p.set_exception(std::current_exception());
    }
}

接收方调用 future 的 get 方法时,异常会被重新抛出。

还可以非阻塞地检查 future 状态:

  • fut.wait_for(timeout):等待一段时间
  • fut.wait_until(time_point):等待到指定时间点
  • fut.valid():判断是否有关联的共享状态

基本上就这些。合理使用 future 和 promise 能简化多线程编程中的数据传递逻辑,避免显式锁的使用,提升代码可读性和安全性。