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.