std::future和std::promise用于线程间安全传递异步结果,其中promise设置值或异常,future获取结果并支持阻塞等待,适用于需精细控制的复杂异步场景。

在c++并发编程中,std::future 和 std::promise 是处理异步任务、获取后台计算结果的重要工具。它们提供了一种线程间传递值的安全方式,无需手动管理互斥锁或条件变量。
std::future 与 std::promise 的基本作用
std::promise 是一个用于设置值的对象,它可以将某个值“承诺”在未来某个时刻提供;而 std::future 则是用来获取这个“承诺”的结果的对象。两者配对使用,常用于一个线程产生结果,另一个线程等待结果的场景。
核心机制:一个线程通过 std::promise::set_value() 设置结果,另一个线程通过 std::future::get() 获取该结果。一旦值被设置,future 就能成功返回结果,否则 get() 会阻塞直到结果可用。
基本使用示例
下面是一个简单的例子,展示如何用 promise 和 future 在两个线程之间传递数据:
立即学习“C++免费学习笔记(深入)”;
#include <iostream> #include <thread> #include <future> void setValue(std::promise<int>&& p) { std::this_thread::sleep_for(std::chrono::seconds(2)); p.set_value(42); // 设置结果 } int main() { std::promise<int> prom; std::future<int> fut = prom.get_future(); // 获取关联的 future std::thread t(setValue, std::move(prom)); std::cout << "等待结果...n"; int result = fut.get(); // 阻塞等待结果 std::cout << "得到结果: " << result << "n"; t.join(); return 0; }
输出:
等待结果…
得到结果: 42
注意:promise 必须通过 move 传递给线程函数,因为它是不可复制的。每个 promise 只能 set_value 一次,多次调用会导致异常。
异常传递与错误处理
除了正常值,promise 还可以传递异常。如果计算过程中发生错误,可以使用 set_exception() 方法将异常保存到 promise 中,future 调用 get() 时会重新抛出该异常。
void mayThrow(std::promise<double>&& p) { try { // 模拟可能出错的操作 throw std::runtime_error("计算失败"); } catch (...) { p.set_exception(std::current_exception()); } }
在主线程中调用 fut.get() 会抛出 runtime_error,可以使用 try-catch 捕获。
与 std::async 的对比
std::async 更高层,自动管理线程和 future,适合简单异步调用。而 promise + future 提供更细粒度控制,适用于复杂场景,比如:
- 需要从回调中设置结果
- 跨多个事件聚合结果
- 与非标准线程(如IO线程、定时器)集成
例如网络请求完成时,由事件系统调用 promise.set_value(),而不是启动新线程。
基本上就这些。合理使用 promise 和 future 能让异步逻辑更清晰,避免竞态条件。