为什么 HPX 要求 future 的 "then" 成为 DAG(有向无环图)的一部分?
Why HPX requires future's "then" to be part of a DAG (directed acyclic graph)?
在 HPX 介绍教程中,您了解到可以使用 future 的 then()
方法,它允许您在 future 准备就绪时将一些操作加入队列。
在这本手册中,在解释如何使用 thens 时,有一句话说 "Used to build up dataflow DAGs (directed acyclic graphs)"。
我的问题是,这个队列必须是非循环的是什么意思?我可以创建一个在 then 中重新计算未来的函数吗?这看起来像 myfuture.then( recompute myfuture ; myfuture.then() )
?
您可以认为 hpx::future
(与 [=12= 非常相似,如果不完全相同,请参阅 https://en.cppreference.com/w/cpp/experimental/future)是匿名生产者和消费者之间的一次性管道。它不代表任务本身,而只代表产生的结果(可能尚未计算)。
因此 'recomputing' 未来(如您所说)只能意味着从异步提供程序(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'
我不确定这是否回答了你的问题以及你为什么要这样做,但是你开始吧。
在 HPX 介绍教程中,您了解到可以使用 future 的 then()
方法,它允许您在 future 准备就绪时将一些操作加入队列。
在这本手册中,在解释如何使用 thens 时,有一句话说 "Used to build up dataflow DAGs (directed acyclic graphs)"。
我的问题是,这个队列必须是非循环的是什么意思?我可以创建一个在 then 中重新计算未来的函数吗?这看起来像 myfuture.then( recompute myfuture ; myfuture.then() )
?
您可以认为 hpx::future
(与 [=12= 非常相似,如果不完全相同,请参阅 https://en.cppreference.com/w/cpp/experimental/future)是匿名生产者和消费者之间的一次性管道。它不代表任务本身,而只代表产生的结果(可能尚未计算)。
因此 'recomputing' 未来(如您所说)只能意味着从异步提供程序(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'
我不确定这是否回答了你的问题以及你为什么要这样做,但是你开始吧。