如何在多线程 C++ 中传递多维数组
how to pass multidimensional array in multithreading c++
我试图创建多线程来处理 2 个多维数组:
vector<thread> tt;
for(int i=0;i<4;i++) tt.push_back(thread(th,arr1,arr2));
使用线程函数:
void th(int arr1[3][100][100], int arr2[100][100]) {
...
}
我也试图通过引用传递,但也没有让它工作:
void th(int (&arr1)[3][100][100], int (&arr2)[100][100]) {
...
}
他们两个都给了我一个"no type named 'type' in 'class std::result_of void(* (int **[])..."
错误。有人可以告诉我如何在多线程中正确传递多维数组吗?
您的原始函数调用对我来说闻起来很奇怪,但以下调用仍然可以编译并使用命令进行g++-4.6.3
运行良好
g++ -lpthread -std=c++0x -g multiArray.cc -o multiArray && ./multiArray
那么multiArray.cc
有
#include <iostream>
#include <thread>
#include <vector>
void th(int ar1[3][100][100], int ar2[100][100])
{
std::cout << "This works so well!n";
}
int main()
{
int ar1[3][100][100];
int ar2[100][100];
std::vector<std::thread> threads;
for(int ii = 0; ii<4; ++ii)
{
threads.emplace_back(th, ar1,ar2);
}
for(auto & t : threads)
{
if(t.joinable())
{
t.join();
}
}
}
让我澄清一下,这段代码是可疑的;例如,如果你改变数组大小(不改变你的数组尺寸),这段代码编译没有问题。
相关文章:
- 如何将元素添加到数组的线程安全函数?
- 将数组作为多线程应用程序中函数的返回传递
- 我可以计算多线程数的平均值吗?
- 多线程文件 IO 程序在线程数增加时行为不可预测
- 当结构数组在主线程中填充数据时,从结构数组的低索引元素读取是否线程安全
- 在多线程程序中使用动态数组的头段安全吗
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 编写std::vector与普通数组的线程安全性
- 为什么对于简单的数组复制来说,没有多线程加速
- C++ 多线程数组
- 支持多线程方法来构建数组中所有元素的集合吗
- 2个数组/图像相乘的多线程性能-英特尔IPP
- 数组中的多线程写入操作
- 如何在多线程 C++ 中传递多维数组
- 在多线程环境中将数据写入数组
- 多线程-作为参数传递的常量字符数组
- 在多线程代码中缓存友好的数组迭代模式
- 多线程程序,它将线程保存在vector而不是数组中
- 多线程和动态数组/矩阵操作
- 为可变核数编写多线程程序