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

在C++中,std::future 和 std::promise 是用于实现异步操作的重要工具,它们定义在 <future> 头文件中。通过这对机制,一个线程可以将结果传递给另一个线程,而不需要显式地使用锁或共享变量。
std::promise 是一个“承诺”对象,它可以设置一个值(或异常),这个值将来会被获取。std::future 则是与 promise 关联的“未来值”,它提供了一个访问该值的方法,通常带有阻塞等待的能力。
std::promise 对象可以通过 get_future() 获取对应的 std::future。set_value() 或 set_exception() 后,future 就能获取结果。下面是一个简单的例子,展示如何用 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)。
除了正常值,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;
}
你可以结合 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;
}
这种方式让你可以自由控制异步逻辑的触发和结果返回时机。
promise 只能设置一次值或异常,重复调用 set_value 会导致程序终止。set_value,future 在调用 get() 时会抛出 std::future_error。std::shared_future 支持多个等待者。基本上就这些。future 和 promise 提供了一种清晰的“生产-消费”模型来处理跨线程结果传递,适合需要精确控制异步流程的场景。
以上就是c++++中future和promise怎么用_future与promise实现异步操作的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号