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

Array of threads and attempting to pass multiple arguments to function is not working?

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

但是,我当前的代码给出了一个错误,我认为这是由于我对二维数组的奇怪使用:

In function 'int main()': 102 77 [Error] no matching function for call to '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 [Note] candidates are: 4 0

133 7 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] template std::thread::thread(_Callable&&, _Args&& ...)

133 7 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] template argument deduction/substitution failed:

102 77 [Note] variable-sized array type 'float (&)[(((sizetype)(((ssizetype)nodeNumber) + -1)) + 1)][2]' is not a valid template argument

4 0 In file included from

128 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] std::thread::thread(std::thread&&)

128 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] candidate expects 1 argument, 5 provided

122 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] std::thread::thread() 122 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32.8.1\include\c++\thread [Note] candidate expects 0 arguments, 5 provided

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

#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]); 
    }
}

对我做错了什么有什么建议吗?帮助将不胜感激!谢谢!

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);
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);
}