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

标签 c++ multithreading stdthread

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

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

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\4.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\4.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\4.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\4.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\4.8.1\include\c++\thread [Note] std::thread::thread() 122 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.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]); 
    }
}

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

最佳答案

thread myThread[nodeNumber];

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

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

不初始化你的线程。

我建议使用线程 vector ,如 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);
}

关于c++ - 线程数组并试图将多个参数传递给函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262894/

相关文章:

c++ - 使用 Opengl 在 Window 上显示菜单

时间:2018-01-08 标签:c++oop: function pointer table

C/海湾合作委员会 : global variables shared between threads need to be volatile?

c++ - 如何在 C++ 中确定一个 win32 线程处于 Wait 或 Join 或 Sleep 状态

java - 识别用于在 Java 中显示频繁数据输入的 Swing 线程

multithreading - Thread_Guard类中std::thread引用的默认值是多少?

c++ - Win32 的用户界面控件

c++ - 意外地能够从基类ctor调用派生类虚函数

c++ - std::thread 有多标准?

c++ - gtkmm:什么是 Glib::RefPtr 用例?