ChuNan
ChuNan

Reputation: 1151

how to pass multidimensional array in multithreading c++

I was trying to create multithreading to handle 2 multi-dimensional arrays:

vector<thread> tt;  
for(int i=0;i<4;i++) tt.push_back(thread(th,arr1,arr2));

with the threaded function :

void th(int arr1[3][100][100], int arr2[100][100]) {
...
}

I also tried to pass by reference yet not get it work as well:

void th(int (&arr1)[3][100][100], int (&arr2)[100][100]) {
    ...
    }

Both of them gives me an "no type named 'type' in 'class std::result_of void(* (int **[])..." error. Can someone please show me how to correctly pass multidimensional arrays in multithreading?

Upvotes: 1

Views: 1189

Answers (1)

user14717
user14717

Reputation: 5181

Your original function call smells strange to me, but still the following call compiles and runs just fine with g++-4.6.3 using the command

g++ -lpthread -std=c++0x -g multiArray.cc -o multiArray && ./multiArray

Then multiArray.cc has

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

Let me clarify that this code is suspect; for instance, if you change the array sizes (without changing your array dimensions) this code compiles without problem.

Upvotes: 2

Related Questions