cdd
cdd

Reputation: 45

Passing a dynamic array to pthreads via a struct

I am working my way through creating pthreads by passing them a struct and running into some issues. With the following code, I can place a set of integers into the struct and then work with them in the thread:

struct v{
    int i;
    int j;
};
void* update(void* param);

int main(int argc, char* argv[]){
    ...
    int j = 2;
    int i = 1;
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct v *argument = (struct v*)malloc(sizeof(struct v));
    argument->i = i;
    argument->j = j;
    pthread_create(&tid, &attr, update, argument);
    ...
    pthread_join(tid, NULL);
    return 0;
}
void* update(void* arg){
    ...
    struct v * argument = (struct v*) arg;
    int j = argument->j;
    int i = argument->i;
    cout << j << ' ' << i << endl;
}

Unfortunately, I do not seem to be able to add a dynamic array to the struct. I understand that dynamic arrays do not work in structs declared before the main(), but even with a pointer I do not seem to be able to get the code to compile. Within main(), I added these lines:

int arr[i][j];

Below

argument->j = j;

I added:

argument.current = arr;

I changed the struct to:

struct v{
    int i;
    int j;
    int *ray;
};

With in the update function, I have:

int * curr = argument->ray;

When I compile, I get an error message "request for member 'ray' in 'argument', which is of non-class type 'v*'".

Am I going down the wrong path by adding this dynamic array this way?

I appreciate any help that any one can provide.

Upvotes: 0

Views: 1184

Answers (1)

Arne Mertz
Arne Mertz

Reputation: 24576

I understand that dynamic arrays do not work in structs declared before the main()

In wich way should they "not work"? It does not matter where you declare/define them as long as you define and use them right.

int arr[i][j]; - this is a VLA, since i and j are not compiletime constants. VLAs are not part of C++03 and C++11, they are a C feature. Something similar will be introduced with C++14.

argument.current = arr;

I changed the struct to:

 struct v{
     int i;
     int j;
     int *ray;
 };

Where is current in that struct? No wonder if it does not compile ;-) (You might want to provide an SSCCE next time).

Enough nitpicking, let's try to deal with your problem:

Two-dimensional arrays cannot be implemented with simple pointers. You could use pointers to pointers instead, e.g. like this:

struct v{
  int i;
  int j;
  int **ray;
};

But since you are using C++, I'd recommend using a vector of vectors or something similar. You can find more about allocation of two-dimensional arrays in This SO answer.

And since you are using C++, you might well be using C++11 or boost, so you have very good chances that std::thread or boost::thread are available, wich a nice-to-use portable wrappers around the threads of your environment, in your case pThread. Your code might look like this then:

void update(std::vector<std::vector<int>>& param) { //or whatever signature suits your needs
  //...
}

int main() {
  int i = 42;
  int j = 11;
  std::vector<std::vector<int>> myVecVec(j, std::vector<int>(j));

  std::thread theThread( [&](){update(myVecVec);} );
  //or: 
  //std::thread theThread( update, std::ref(myVecVec) );

  //...

  theThread.join();
}

No fiddling with thread internals, no manual memory management needed.

Upvotes: 1

Related Questions