来源:https://www.runoob.com/cplusplus/cpp-libs-future.html
C++ 标准库
C++11 引入了 <future>
头文件,它提供了一种异步编程的机制,允许程序在等待某个操作完成时继续执行其他任务。<future>
库是 C++ 标准库中并发编程的一部分,它允许程序员以一种更简洁和安全的方式处理异步操作。
<future>
库中定义了几个关键的类型:
std::future
:表示异步操作的结果,可以查询操作的状态,获取结果或等待操作完成。std::promise
:用于与 std::future 配对,用于设置异步操作的结果。std::packaged_task
:封装一个函数或可调用对象,使其可以作为异步任务执行。std::promise
std::promise
用于设置异步操作的结果。它与 std::future 配对使用。
异步调用
参考:https://en.cppreference.com/w/cpp/thread/future
要点
std::async(std::launch::async, [](){})
返回的是一个std::future<T>
类型的,可以直接用vector
的push_back()
放入队列后面
std::vector<std::future<T>> vs;
vs.push_back(std::async(...);
但是不可以将
函数std::async()
的返回值复制给变量后再 push_back()
到队列。
std::future<T>
变量传来传去,可能会导致其中的T
数据类型的复制和析构出现问题,所以尽量使用stared_ptr<T>
吧
{
std::vector<std::future<std::shared_ptr<Json::Value>>> ress;
for(auto &[k, v] : platform_uids) {
auto platform = k;
auto searcher = searchers_[platform];
ress.push_back(std::async(std::launch::async, [&searcher, &v]() { //直接将返回的右值 push_back 到队列后面
auto res = searcher->Search(v);
//Json::Value res;
return std::make_shared<Json::Value>(res);
}));
}
for(auto &r : ress) {
Json::Value t;
auto pres = r.get(); // shared_ptr
for(int i = 0; i < pres->size(); i++) {
rep.append(pres->get(i, t));
}
}
}
几个实例
实例
#include <iostream>
#include <future>
int main() {
std::promise<int> prom;
std::future<int> fut = prom.get_future();
// 在另一个线程中设置结果
std::thread t([prom]() {
prom.set_value(10);
});
// 等待结果
std::cout << "Future value: " << fut.get() << std::endl;
t.join();
return 0;
}
输出结果:
Future value: 10
std::packaged_task
std::packaged_task 封装一个函数或可调用对象,使其可以作为异步任务执行。
实例
#include <iostream>
#include <future>
#include <cmath>
int compute_square_root(double x) {
return std::sqrt(x);
}
int main() {
std::packaged_task<double(double)> task(compute_square_root);
std::future<double> result = task.get_future();
std::thread th(std::move(task), 9.0);
std::cout << "Result: " << result.get() << std::endl;
th.join();
return 0;
}
输出结果:
Result: 3
std::async
std::async 是一个方便的函数,用于启动异步任务。它可以立即返回一个 std::future 对象。
实例
#include <iostream>
#include <future>
int main() {
std::future<int> fut = std::async(std::launch::async, [](int x) {
return x * x;
}, 5);
std::cout << "Result: " << fut.get() << std::endl;
return 0;
}
输出结果:
Result: 25
异常处理
当异步操作抛出异常时,std::future 会捕获这个异常,并且可以通过调用 .get() 方法来重新抛出它。
实例
#include <iostream>
#include <future>
void throw_exception() {
throw std::runtime_error("Exception thrown");
}
int main() {
std::future<void> fut = std::async(throw_exception);
try {
fut.get();
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << std::endl;
}
return 0;
}
输出结果:
Caught exception: Exception thrown
<future>
库为 C++ 程序员提供了一种简单而强大的异步编程方式。通过使用 std::promise
、std::packaged_task
和 std::async
,我们可以轻松地在 C++
程序中实现并发和异步操作。同时,异常处理机制也确保了程序的健壮性。