std::future和std::promise用于C++多线程间异步通信,实现生产者-消费者模式;std::promise设值或异常,std::future获取结果,二者通过get_future关联,支持异常传递与移动语义,需注意仅能设置一次且避免未设值销毁。

在C++多线程编程中,std::future 和 std::promise 是一对用于实现异步通信的重要工具。它们配合使用可以在线程之间传递结果或异常,实现一种“生产者-消费者”模式的数据传递机制。
基本概念:future 与 promise 的作用
std::promise 是一个可写入一次的容器,用于保存某个值或异常;而 std::future 是与之关联的只读对象,用于在未来某个时间点获取这个值。每个 promise 都可以通过 get_future() 获取对应的 future。
典型场景是一个线程计算结果并将其设置到 promise 中,另一个线程通过对应的 future 等待并获取该结果。
基本用法示例
下面是一个简单的例子,展示两个线程如何通过 promise 和 future 进行通信:
立即学习“C++免费学习笔记(深入)”;
#include#include #include void producer(std::promise && prms) { try { // 模拟耗时操作 std::this_thread::sleep_for(std::chrono::seconds(2)); int result = 42; prms.set_value(result); // 设置结果 } catch (...) { prms.set_exception(std::current_exception()); } } void consumer(std::future && fut) { std::cout << "等待结果...\n"; int value = fut.get(); // 阻塞直到结果可用 std::cout << "收到结果:" << value << "\n"; } int main() { std::promise prms; std::future fut = prms.get_future(); std::thread t1(producer, std::move(prms)); std::thread t2(consumer, std::move(fut)); t1.join(); t2.join(); return 0; }
在这个例子中:
- 主线程创建了一个 promise 和其对应的 future。
- producer 线程负责生成数据,并通过 set_value() 将结果写入 promise。
- consumer 线程调用 get() 在 future 上阻塞等待,直到值可用。
异常传递支持
promise 不仅能传递正常值,还能传递异常。如果计算过程中发生错误,可以使用 set_exception() 方法将异常保存进去,future 在 get() 时会重新抛出该异常。
例如:
void faulty_producer(std::promise&& prms) { try { throw std::runtime_error("计算失败"); } catch (...) { prms.set_exception(std::current_exception()); } }
当 consumer 调用 fut.get() 时,会抛出 runtime_error,需用 try-catch 处理。
实用技巧与注意事项
使用 promise 和 future 时需要注意以下几点:
- 每个 promise 只能调用一次 set_value、set_exception 或 set_exception_at_thread_exit(否则会抛出 std::future_error)。
- 若 promise 被销毁前未设置值,且没有被“放弃”(如调用 std::promise::set_value()),则其 future 会在 get() 时抛出 std::future_error。
- 移动语义是必须的:promise 和 future 都不可复制,只能移动。
- 可用于线程间解耦:发送方只需关心设置结果,接收方只关注获取结果,无需显式锁。
基本上就这些。合理使用 promise 和 future,可以让多线程任务的结果传递更清晰安全。虽然功能强大,但也注意避免死锁——比如不要在等待 future 的同时持有锁,导致生产者无法完成写入。










