1

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?

5
  • 4
    Take out the multithreading and focus on getting your function declarations and calls right. Commented Jan 2, 2015 at 17:50
  • tt.push_back(thread(th, std::ref(arr1), std::ref(arr2))); with std::ref and th taking references to arrays (so void th(int (&arr1)[3][100][100], int (&arr2)[100][100])) Commented Jan 2, 2015 at 18:03
  • ideone.com/RkR40P See how easy it is with std::array. Never use raw arrays Commented Jan 2, 2015 at 18:07
  • @PiotrS. Thanks Piotr. I tried ref indeed, however, got the same error. Commented Jan 2, 2015 at 18:07
  • 2
    @ChuNan please attach the MCVE Commented Jan 2, 2015 at 18:11

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Can we also pass by reference?
I assume it's faster if the array values are changed?
@NickThompson a pointer to array drops its first dimension, a reference doesn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.