使用 std::async 准备好后立即显示结果

Displaying results as soon as they are ready with std::async

本文关键字:显示 结果 准备好 std async 使用      更新时间:2023-10-16

我正在尝试发现C++中的异步编程。这是我一直在使用的一个玩具示例:

#include <iostream>
#include <future>
#include <vector>
#include <chrono>
#include <thread>
#include <random>
// For simplicity
using namespace std;
int called_from_async(int m, int n)
{
this_thread::sleep_for(chrono::milliseconds(rand() % 1000));
return m * n;
}
void test()
{
int m = 12;
int n = 42;
vector<future<int>> results;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
results.push_back(async(launch::async, called_from_async, i, j));
}
}
for(auto& f : results)
{
cout << f.get() << endl;
}
}

现在,这个例子并不有趣,但它提出了一个对我来说很有趣的问题。假设我想在结果"到达"时显示结果(我不知道首先准备好什么,因为延迟是随机的),我应该怎么做?

我在这里所做的显然是错误的,因为我按照创建它们的顺序等待所有任务 - 所以我会等待第一个任务完成,即使它比其他任务长。

我想到了以下想法:对于每个future,在短时间内使用wait_for,如果准备就绪,则显示值。但我觉得这样做很奇怪:

while (any_of(results.begin(), results.end(), [](const future<int>& f){
return f.wait_for(chrono::seconds(0)) != future_status::ready;
}))
{
cout << "Loop" << endl;
for(auto& f : results)
{
auto result = f.wait_for(std::chrono::milliseconds(20));
if (result == future_status::ready)
cout << f.get() << endl;
}
}

这带来了另一个问题:我们会在某些future上多次调用get,这是非法的:

在抛出 'std::future_error'
的实例后调用终止 what(): std::future_error:无关联状态

所以我真的不知道该怎么做,请建议!

使用valid()跳过您已经调用get()的未来。

bool all_ready;
do {
all_ready = true;
for(auto& f : results) {
if (f.valid()) {
auto result = f.wait_for(std::chrono::milliseconds(20));
if (result == future_status::ready) {
cout << f.get() << endl;
}
else {
all_ready = false;
}
}
}
}
while (!all_ready);