【问题标题】:Array of threads and attempting to pass multiple arguments to function is not working?线程数组并尝试将多个参数传递给函数不起作用?
【发布时间】:2016-01-20 15:52:47
【问题描述】:

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

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

In function 'int main()': 102 77 [Error] no matching function for call to 'std::thread::thread(void (&)(float ()[2], float, int, int), 浮动 [(((sizetype)(((ssizetype)nodeNumber) + -1)) + 1)][2], 浮动 [(((sizetype)(((ssizetype)nodeNumber) + -1)) + 1)], int&, int&)'

102 77 [注] 候选人为:4 0

133 7 c:\程序文件 (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread [注意] 模板 std::thread::thread(_Callable&&, _Args&& ...)

133 7 c:\程序文件 (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:\程序文件 (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread [注意] std::thread::thread(std::thread&&)

128 5 c:\程序文件 (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread [注意] 候选人需要 1 个参数,提供 5 个

122 5 c:\程序文件 (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread [注意] std::thread::thread() 122 5 c:\程序文件 (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]); 
    }
}

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

【问题讨论】:

  • 我为您编辑了错误消息。您始终可以选择要阻止引用的所有内容,然后按“按钮
  • 谢谢,下次我会努力记住的!

标签: c++ multithreading stdthread


【解决方案1】:
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);
}

【讨论】:

  • 感谢您的回复。我试过你的建议,但我得到这个错误:[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&)'
  • 我包含了向量库。我想知道它是否不喜欢我的地图功能。基本上,adjacency_matrix 是一个二维数组,但我已经破解了它,所以我只传入行并进行计算。当我测试它时,它不使用线程就可以工作。现在我只是尝试使用线程在数组的各个部分之间拆分工作(使用节点)。
  • 您能否编辑您现有的帖子或提出其他问题并发布mcve。发布足够的代码让我们启动它并查看问题所在。
  • 我认为问题在于可变大小的数组 adjacency_matrix。
  • 您的示例是否为您编译?因为它不适合我。
【解决方案2】:
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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 2016-10-05
    • 2022-08-05
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多