首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么HPX需要未来的"then“成为DAG (有向无环图)的一部分?

为什么HPX需要未来的"then“成为DAG (有向无环图)的一部分?
EN

Stack Overflow用户
提问于 2019-05-03 23:56:33
回答 1查看 73关注 0票数 0

在HPX入门教程中,您可以了解到您可以使用future的then()方法,该方法允许您在将来准备好时将一些要计算的操作排入队列。

在这本手册中,有一句话是在解释如何使用"Used to build up dataflow DAGs (directed acyclic graphs)"时说的。

我的问题是,这个队列必须是非循环的,这意味着什么?我能做一个在then中重新计算未来的函数吗?这看起来像myfuture.then( recompute myfuture ; myfuture.then() )吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-04 20:57:33

您可以认为hpx::future (非常类似于std::experimental::future,如果不是完全相同,请参见https://en.cppreference.com/w/cpp/experimental/future)是匿名生产者和消费者之间的一次性管道。它不表示任务本身,而只是生成的结果(可能还没有计算出来)。

因此,“重新计算”未来(正如您所说的)只能意味着从异步提供程序(hpx::asyncfuture<>::then等)重新初始化未来。

代码语言:javascript
复制
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'

我不确定这是否回答了你的问题,以及你为什么想要这样做,但你可以这样做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55973380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档