【问题标题】:What is the correct way to use hpx::when_any function?使用 hpx::when_any 函数的正确方法是什么?
【发布时间】:2023-01-31 09:08:51
【问题描述】:

我刚刚开始学习 C++ 并行编程,并想为此使用 HPX。我需要以 N 为一组完成几项任务,我想编写将所有线程放入一个向量中的代码,并且当至少完成一个线程时将其替换为下一个线程。

#include <iostream>
#include <vector>
#include "hpx/hpx_main.hpp"
#include "hpx/future.hpp"

using namespace std;



int dummy(int a){
    return a;
    }
    
int main(){
    vector<hpx::future<int>>futures;
    futures.reserve(3);
    
    for(int step = 0; step < 3; step++){
        futures.push_back(hpx::async(dummy, step));
        }
    
    int index;
    auto f2 = hpx::when_any(futures).then([&](auto f){
        return f;
        });
        
    
    auto res = f2.get();
    vector<hpx::future<int>> fut3 = res.futures;
    for(int i = 0; i < fut3.size(); i++){
        cout << fut3[i].get() << endl;
        }
    }

此代码导致以下错误:

错误:静态断言失败:结果类型必须可从输入类型构造

我试图在网上找到解决方案,但几乎没有任何使用 hpx 的代码示例。

【问题讨论】:

  • 考虑到很多 std:: 设施在 hpx:: 下重新实现并且使用非限定名称可能会使编译器混淆,因此在编写 HPX 代码时不要使用 using namespace std; 是有说服力的;)
  • 现在想来,无论HPX如何,一般都使用不合格的名字并不是什么好事。

标签: c++ parallel-processing hpx


【解决方案1】:

尝试

std::vector&lt;hpx::future&lt;int&gt;&gt; fut3 = std::move(res.futures);

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 2019-12-03
    • 2010-11-10
    • 1970-01-01
    • 2019-05-07
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多