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

在C++11中,std::future 和 std::promise 是实现异步操作和线程间通信的重要工具。它们提供了一种机制,使得一个线程可以将结果传递给另一个线程,而无需共享变量或使用互斥锁进行复杂同步。
std::promise 是一个可写入一次的对象,用于设置某个值或异常;std::future 是与之关联的只读对象,用于获取这个值。两者通过共享状态连接,通常用于跨线程传递结果。
典型使用场景包括:
使用 std::async 可以快速启动异步任务,并自动返回一个 std::future:
立即学习“C++免费学习笔记(深入)”;
#include <future>
#include <iostream>
int compute() {
return 42;
}
int main() {
std::future<int> fut = std::async(compute);
int result = fut.get(); // 阻塞直到结果可用
std::cout << "Result: " << result << "\n";
return 0;
}
这里 fut.get() 会等待任务完成并取出结果。如果任务抛出异常,调用 get 会重新抛出该异常。
当需要更精细控制时,可以手动绑定 promise 和 future:
#include <thread>
#include <future>
#include <iostream>
void set_value(std::promise<int>&& prom) {
prom.set_value(100); // 设置结果
}
int main() {
std::promise<int> prom;
std::future<int> 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<double>&& p) {
try {
throw std::runtime_error("Something went wrong");
} catch (...) {
p.set_exception(std::current_exception());
}
}
接收方调用 future 的 get 方法时,异常会被重新抛出。
还可以非阻塞地检查 future 状态:
基本上就这些。合理使用 future 和 promise 能简化多线程编程中的数据传递逻辑,避免显式锁的使用,提升代码可读性和安全性。
以上就是C++如何使用future和promise_C++11中实现异步操作与线程间通信的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号