在HPX入门教程中,您可以了解到您可以使用future的then()方法,该方法允许您在将来准备好时将一些要计算的操作排入队列。
在这本手册中,有一句话是在解释如何使用"Used to build up dataflow DAGs (directed acyclic graphs)"时说的。
我的问题是,这个队列必须是非循环的,这意味着什么?我能做一个在then中重新计算未来的函数吗?这看起来像myfuture.then( recompute myfuture ; myfuture.then() )吗?
发布于 2019-05-04 20:57:33
您可以认为hpx::future (非常类似于std::experimental::future,如果不是完全相同,请参见https://en.cppreference.com/w/cpp/experimental/future)是匿名生产者和消费者之间的一次性管道。它不表示任务本身,而只是生成的结果(可能还没有计算出来)。
因此,“重新计算”未来(正如您所说的)只能意味着从异步提供程序(hpx::async、future<>::then等)重新初始化未来。
hpx::future<int> f = hpx::async([]{ return 42; });
hpx::future<int> f2 = f.then(
[](hpx::future<int> r)
{
// this is guaranteed not to block as the continuation
// will be called only after `f` has become ready (note:
// `f` has been moved-to `r`)
int result = r.get();
// 'reinitialize' the future
r = hpx::async([]{ return 21; });
// ...do things with 'r'
return result;
});
// f2 now represents the result of executing the chain of the two lambdas
std::cout << f2.get() << '\n'; // prints '42'我不确定这是否回答了你的问题,以及你为什么想要这样做,但你可以这样做。
https://stackoverflow.com/questions/55973380
复制相似问题