views:

155

answers:

1

Hi,

I'm actually writing an MPI program. This is a basic client / server pattern. The server have a set of work to compute. The clients get subsets of this big set. Each client use several threads to compute the subset. I must be sure all the threads finished before requesting another subset to the server.

The client is split into several threads: a master (for the communications) and several workers.

Here my actual code, where the deadlock should occur. I cleaned it of any MPI calls, to make it more readable. I really can not see what I am doing wrong here.

void* worker ( void * arg ) {
  while (work != 0) {
    int x;
    while ( get_job(&x) ) {
      do_job(x);
    }
    pthread_mutex_lock(&mutex_worker);
    nb_job_empty++;
    if (nb_job_empty == NB_THREADS) {
      nb_job_empty = 0;
      pthread_cond_signal(&cond_master);
    }
    nb_worker_woken--;
    pthread_cond_wait(&cond_worker, &mutex_worker);
    nb_worker_woken++;
    pthread_mutex_unlock(&mutex_worker);
  }
    return ( void * ) 0 ;
}

void server() {
    for (int i = 0; i < 100; i++) {
      add_job();
      pthread_mutex_lock(&mutex_master);
      pthread_cond_broadcast(&cond_worker);
      pthread_cond_wait(&cond_master, &mutex_master);
      pthread_mutex_unlock(&mutex_master);
    }
    work = 0;
    pthread_mutex_lock(&mutex_master);
    pthread_cond_broadcast(&cond_worker);
    pthread_mutex_unlock(&mutex_master);
}
+2  A: 

You should use the same mutex for both condition (mutex_master and mutex_worker should be the same)

Moreover, you should use pthread_cond_wait in a while loop as it is recommended :)

claferri