note: I'm currently using mac os X so I couldn't use the pthread_barrier primitive.
I'm trying to hold a 'n' number of threads to do a matrix computation on two matrices. Only once every thread has been created or is 'ready' the first thread will start computing the appropriate subset of the matrix and then the second thread and so on. (I'm trying to simulate a relay race, so this would be one team) I'm just trying to get it working with one team.
Anyways here it seems where my problem is rooted:
Code:
//creating start method right now just a prototype
//this method will be implemented as a "barrier"
void* start(void* input){
long id = (long)input;
//maybe I will need to create a buffer here to hold the threads
if(id == k -1){
pthread_cond_signal(&testCond);
}
//create a mutex lock here
pthread_mutex_lock(&theMutex);
//set boolean predicate for condition variable
//while the last thread has not come in let's wait for others to arrive.
while(id < k) pthread_cond_wait(&testCond,&theMutex);
compute(id); // call compute function
//unlock the mutex
pthread_mutex_unlock(&theMutex);
pthread_exit(0);
}
Also, not that the function compute() it's just a function to perform the correct subset multiplication of the two matrices
and here is most of my main:
Code:
int main(int argc, char** args) {
// Read the number of threads that need to be created for each team from the command line.
k = atoi(args[1]);
//test the input
printf("Number of threads is: %d\n",k);
// Populate the A matrix data.
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = i;
}
}
// Populate the B matrix data.
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
B[i][j] = i;
}
}
//create an array of threads for each team
pthread_t* redThreads = malloc(k*sizeof(pthread_t));
pthread_t* blueThreads = malloc(k*sizeof(pthread_t));
for(long i = 0; i < k;i++){
//create one of each team
pthread_create(&redThreads[i],0,start,(void*)(long)i);
//pthread_create(&blueThreads[i],0,start,(void*)(long)i);
}
//wait for the threads
for(int i =0; i < k; i++) {
pthread_join(redThreads[i],0);
//pthread_join(blueThreads[i],0);
}
// Print out the matricies.
Now when I run this code the program just keeps on running and no computation is done, so there seems that I'm not using the pthread condition variables correctly? I'm just trying to figure out why my program is getting "hanged up".
Any help is greatly appreciated.