首页 文章

线程数组和尝试将多个参数传递给函数不起作用?

提问于
浏览
0

我正在创建一个具有动态线程数的程序 . 我有一个线程的向量(谢谢,Mohamad);然后我尝试调用一个函数并为执行线程传递多个参数 .

但是,我当前的代码给出了一个错误,我认为是由于我奇怪地使用了2D数组:

在函数'int main()'中:102 77 [错误]没有匹配函数用于调用'std :: thread :: thread(void(&)(float()[2],float,int,int),float [(((sizetype)(((ssizetype)nodeNumber)-1))1)] [2],float [(((sizetype)(((ssizetype)nodeNumber)-1))1)],int&,int&) '102 77 [注意]候选人是:4 0 133 7 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ include \ c \ thread [注意] template std :: thread :: thread(Callable &&, Args && ...)133 7 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ include \ c \ thread [注意]模板参数推导/替换失败:102 77 [注意]变量大小的数组类型'float(&)[(((sizetype)(((ssizetype)nodeNumber)-1))1)] [2] '不是有效的模板参数4 0包含在128 5 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ include \ c \ thread [注意] std :: thread :: thread(std :: thread &&)128 5 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ incl ude \ c \ thread [注意]候选人需要1个参数,5个提供122个5 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ include \ c \ thread [注意] std :: thread :: thread()122 5 c:\ program files(x86)\ dev-cpp \ mingw64 \ lib \ gcc \ x86_64-w64-mingw32 \ 4.8.1 \ include \ c \ thread [注意]候选人需要0个参数,5个提供

以下是我尝试此操作的一些代码块:

#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <vector>
using namespace std;


void map(float rank_matrix[][2], float adjacency_matrix[], int nodeNumber, int node);

int main()  {
    // setup code, initialization section here

        float adjacency_matrix[nodeNumber][nodeNumber];
        float rank_matrix[nodeNumber][2];

    while(iter < terminate)  {

        vector<thread> threads;

        for(int i = 0; i < nodeNumber; i++)  {
            threads.push_back(std::thread(map, rank_matrix, adjacency_matrix[i], nodeNumber, i);
        }

        for(int i = 0; i < nodeNumber; i++)  {
            threads.join();
        }

        // Flush out the mass for each node (will be updated based on mapper's work.
        for(i = 0; i < nodeNumber; i++)  {
            rank_matrix[i][0] = 0;
        }

        iter++;
        cout << endl;
        cout << endl;
    }

    return 0;
}


// Mapper code for each individual node and computation.
void map(float rank_matrix[][2], float adjacency_matrix[], int nodeNumber,    int node)  {
    for(int i = 0; i < nodeNumber; i++)  {
        if(rank_matrix[node][1] != 0 && adjacency_matrix[i] > 0)
            adjacency_matrix[i] = (rank_matrix[node][0] / rank_matrix[node][1]); 
    }
}

关于我做错的任何建议?非常感谢帮助!谢谢!

2 回答

  • 2
    thread myThread[nodeNumber];
    

    这会创建许多默认初始化的线程,即不代表任何执行线程的线程 .

    myThread[i](map, rank_matrix, adjacency_matrix[i], nodeNumber, i);
    

    不初始化您的线程 .

    我建议使用像这样的example的线程向量

    std::vector<std::thread> threads;
    

    这不会创建任何实际线程 . 只是拿着它们的容器 .

    然后你可以像这样填充它:

    for(int i = 0; i < nodeNumber; i++)  {
        threads.push_back(std::thread(map, rank_matrix, adjacency_matrix[i], nodeNumber, i);
    }
    
  • 3
    myThread[i](map, rank_matrix, adjacency_matrix[i], nodeNumber, i);
    

    正在尝试使用提供的参数调用 myThread[i] 上的函数调用操作符而不调用构造函数 . thread myThread[nodeNumber]; 已经构造了线程,因此您需要做的是为数组的每个元素分配一个线程

    myThread[i] = thread(map, rank_matrix, adjacency_matrix[i], nodeNumber, i);
    

相关问题