user100000 user100000 - 2 months ago 24
C Question

Am I using structs correctly?

I'm learning multithreading in C currently. For this assignment I have to make a multithreaded Conway's Game of Life. The program takes in a bunch of parameters at execution. Here's all the variables it takes in before execution:

./gameoflife <width> <height> <seed> <p> <freq> <#worker_threads>

Anyway, in order to make it multithreaded I decided to split the workload vertically between the #worker_threads. So each worker thread has its own widthStart and widthEnd to work on.

Here's what my data structures look like :

typedef struct {

int width;
int height;
int ** board;

int freq;
int nbThreads;

int * escaped;

} masterStruct;

typedef struct {

int widthStart;
int widthEnd;
int threadID;
masterStruct * master; //this is supposed to be a pointer to the struct above.


Now in my "threader" thread (which is in charge of figuring out widthStart and widthEnd) takes in masterStruct as argument. Inside it, I call a bunch of pthread_creates to whom I'll feed paramsStruct as an argument. I'd like all these newly created pthreads to all have access to the same global masterStruct. So in my "threader" thread, I declared it as such :

paramStruct p[workers]; //create as many paramStruct as nbThreads

for (int i = 0; i < workers; i++) {

*(p[i].master) = myStr; //give each thread

My question is : Is this the correct way to ensure all the pthreads have access to the same masterStruct?

Any help is greatly appreciated.


I thing it's not correct. *(p[i].master) = myStr; copies myStr to some (and I think, invalid) memory. Maybe you want like this: p[i].master = &myStr;.