I'm trying to get multiple threads to perform parallel calculations on a 2D array. The user specifies how many threads they want so on a 25*25 2d array if the user wants 5 threads then each thread performs calculation on 125 elements. (for simplicity I hardcoded these number just to try and get program to work under these conditions).
The code works for 1 thread and when I simulated with a for loop everything works correctly. It's a conways game of life program. With 1 thread or forloop calling gen function 5 times the programs works fine. It prints out the grids properly. With 5 threads it just prints out once and program ends
I can't test inside the threads because printf doesn't work in threads. I've spent hours on this and I can't figure it out.
int N;
int **gridA;// odd generations
int **gridB;//even
int T = 5;//number of threads
int main ( int argc, char *argv[] ){
int i,j;
const int STACK_SIZE = 65536;
char *stack;
char *stackTop[t];
pid_t cret[t], wret;
N = 25;//array size [25][25];
//initialize stack
stack = malloc(STACK_SIZE);
for(i = 0; i < T; i++){
stackTop[i] = stack + STACK_SIZE;
}
//initilize arrays and load gridA with input
while(1){}
for(i=0; i < T; i++) cret[i]=clone(generateNext, stackTop[i], CLONE_VM|SIGCHLD, (void*)i);//thread code
for(i=0; i < T; i++) waitpid(cret[i],&status,0);//wait for threads to finish
//for(i=0; i < T; i++){generateNext((void*)i);} Simulate threads, works like this
if(toggle){//grids used interchangeably.
print_array(gridA);
toggle = 0;
} else {
print_array(gridB);
toggle = 1;
}
}
}
//figures out the next generation
void generateNext(void *p){
//finds out the two points each thread will calculate for by using p
//eg first thread: gridA[0][24] to [4][24] 2nd thread: [5][25] to 9[25]
//then finds neighbours and changes state of each element accordingly
}