std::future和std::promise用于线程间异步传递结果,promise通过set_value设置值,future通过get获取结果,二者通过get_future关联,实现无锁数据传递。

在C++中,std::future 和 std::promise 是用于实现异步操作的重要工具,它们定义在 <future> 头文件中。通过这对机制,一个线程可以将结果传递给另一个线程,而不需要显式地使用锁或共享变量。
1. 基本概念:future 与 promise 的关系
std::promise 是一个“承诺”对象,它可以设置一个值(或异常),这个值将来会被获取。std::future 则是与 promise 关联的“未来值”,它提供了一个访问该值的方法,通常带有阻塞等待的能力。
- 每个
std::promise对象可以通过get_future()获取对应的std::future。 - 当 promise 调用
set_value()或set_exception()后,future 就能获取结果。
2. 基本使用示例
下面是一个简单的例子,展示如何用 promise 在一个线程中设置值,另一个线程通过 future 获取结果:
#include <iostream>
#include <future>
#include <thread>
void set_value(std::promise<int>&& prms) {
std::this_thread::sleep_for(std::chrono::seconds(2));
prms.set_value(42); // 设置结果
}
int main() {
std::promise<int> prom;
std::future<int> fut = prom.get_future(); // 获取关联的 future
std::thread t(set_value, std::move(prom));
std::cout << "等待结果...\n";
int value = fut.get(); // 阻塞直到值可用
std::cout << "得到结果: " << value << "\n";
t.join();
return 0;
}
输出:
立即学习“C++免费学习笔记(深入)”;
等待结果... 得到结果: 42
在这个例子中,主线程调用 fut.get() 会一直等待,直到子线程调用 prms.set_value(42)。
3. 异常传递
除了正常值,promise 还可以设置异常,future 在获取时会重新抛出:
void throw_exception(std::promise<double>&& p) {
try {
throw std::runtime_error("出错了!");
} catch (...) {
p.set_exception(std::current_exception());
}
}
int main() {
std::promise<double> prom;
std::future<double> fut = prom.get_future();
std::thread t(throw_exception, std::move(prom));
try {
double val = fut.get(); // 这里会抛出异常
} catch (const std::exception& e) {
std::cout << "捕获异常: " << e.what() << "\n";
}
t.join();
return 0;
}
4. 实现异步任务的封装
你可以结合 std::async 或手动创建线程,配合 promise 来封装异步操作。例如,模拟一个异步数据加载:
std::future<std::string> load_data_async() {
std::promise<std::string> prom;
std::future<std::string> fut = prom.get_future();
std::thread([p = std::move(prom)]() mutable {
std::this_thread::sleep_for(std::chrono::milliseconds(800));
p.set_value("数据加载完成");
}).detach(); // 注意:生产环境建议管理线程生命周期
return fut;
}
int main() {
auto fut = load_data_async();
std::cout << "正在加载...\n";
std::cout << fut.get() << "\n";
return 0;
}
这种方式让你可以自由控制异步逻辑的触发和结果返回时机。
5. 注意事项
- 每个
promise只能设置一次值或异常,重复调用set_value会导致程序终止。 - 如果 promise 被销毁前没有调用
set_value,future 在调用get()时会抛出std::future_error。 - 移动语义很重要:promise 不可复制,只能移动。
- 避免 detach 线程带来的资源管理问题,必要时使用
std::shared_future支持多个等待者。
基本上就这些。future 和 promise 提供了一种清晰的“生产-消费”模型来处理跨线程结果传递,适合需要精确控制异步流程的场景。










