In HPX introduction tutorials you learn that you can make use of future's then()
method, that allows you to enqueue some operation to be computed when the future is ready.
In this manual there is a sentence that says "Used to build up dataflow DAGs (directed acyclic graphs)" when explaining how to use thens.
My question is, what does it mean that this queue has to be acyclic? Can I make a function that re-computes a future inside a then? This would look like myfuture.then( recompute myfuture ; myfuture.then() )
?
You can think of a hpx::future
(very similar, if not identical to std::experimental::future
, see https://en.cppreference.com/w/cpp/experimental/future) is an one-shot pipeline between an anonymous producer and a consumer. It does not represent the task itself but just the produced result (that might not have been computed yet).
Thus 'recomputing' a future (as you put it) can only mean to reinitialize the future from an asynchronous provider (hpx::async
, future<>::then
, etc.).
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'
I'm not sure if this answers your question and why you would like to do that, but here you go.