std::future和std::promise用于C++异步编程,前者获取结果,后者设置结果;通过创建promise、获取future、启动线程、设置值或异常、最后get获取结果实现;get阻塞可用wait_for避免;异常通过set_exception传递;shared_future允许多次get;async封装了future/promise简化使用;需注意数据线程安全及promise生命周期。

C++ 中
std::future
std::promise
std::promise
std::future
解决方案
要使用
std::future
std::promise
创建 std::promise
std::promise
std::promise<int>
立即学习“C++免费学习笔记(深入)”;
获取 std::future
std::promise
std::future
std::future
启动异步任务: 将异步任务提交给一个新线程或线程池。在这个任务中,你需要使用
std::promise
设置结果: 在异步任务中,使用
std::promise::set_value()
std::promise::set_exception()
获取结果: 在主线程中,使用
std::future::get()
get()
get()
下面是一个简单的示例:
#include <iostream>
#include <future>
#include <thread>
int calculate_sum(int a, int b) {
// 模拟耗时操作
std::this_thread::sleep_for(std::chrono::seconds(2));
return a + b;
}
int main() {
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread t([&promise]() {
try {
int result = calculate_sum(5, 3);
promise.set_value(result);
} catch (...) {
promise.set_exception(std::current_exception());
}
});
try {
int sum = future.get();
std::cout << "Sum: " << sum << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
t.join();
return 0;
}副标题1
std::future
get()
可以使用
std::future::wait_for()
std::future::wait_until()
std::future_status
#include <iostream>
#include <future>
#include <chrono>
#include <thread>
int main() {
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread t([&promise]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
promise.set_value(42);
});
// 等待最多2秒
auto status = future.wait_for(std::chrono::seconds(2));
if (status == std::future_status::ready) {
std::cout << "Result: " << future.get() << std::endl;
} else if (status == std::future_status::timeout) {
std::cout << "Timeout: Result not available yet." << std::endl;
} else {
std::cout << "Deferred." << std::endl;
}
t.join();
return 0;
}副标题2
std::promise
std::future
std::promise
set_exception()
std::future
get()
std::current_exception()
std::promise
#include <iostream>
#include <future>
#include <thread>
#include <stdexcept>
int main() {
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread t([&promise]() {
try {
throw std::runtime_error("Something went wrong in the thread!");
} catch (...) {
promise.set_exception(std::current_exception());
}
});
try {
future.get(); // 这会抛出 std::runtime_error
} catch (const std::runtime_error& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
t.join();
return 0;
}副标题3
std::shared_future
std::future
std::shared_future
std::future
get()
std::shared_future
get()
wait()
如果你需要多个线程访问同一个异步操作的结果,应该使用
std::shared_future
std::shared_future
#include <iostream>
#include <future>
#include <thread>
#include <vector>
int main() {
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::shared_future<int> shared_future = future.share();
std::vector<std::thread> threads;
for (int i = 0; i < 3; ++i) {
threads.emplace_back([shared_future, i]() {
std::cout << "Thread " << i << ": " << shared_future.get() << std::endl;
});
}
promise.set_value(123);
for (auto& t : threads) {
t.join();
}
return 0;
}副标题4
std::async
std::future/std::promise
std::async
std::future
std::promise
std::async
std::promise
std::promise
std::future
std::async
std::promise
std::thread
std::future
std::promise
#include <iostream>
#include <future>
int calculate_product(int a, int b) {
std::cout << "Calculating product in a separate thread." << std::endl;
return a * b;
}
int main() {
std::future<int> future = std::async(std::launch::async, calculate_product, 7, 6);
std::cout << "Waiting for the result..." << std::endl;
int product = future.get();
std::cout << "Product: " << product << std::endl;
return 0;
}副标题5
使用
std::future
std::promise
std::promise
std::future
std::promise
std::promise
set_value
set_exception
以上就是c++++如何使用std::future和std::promise_c++异步编程future/promise指南的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号