title: Starting all threads at the same time
body: <p>I'm having trouble understanding condition variables like <code>pthread_mutex_lock</code>/<code>unlock</code> and <code>pthread_cond_wait</code>/<code>signal</code></p>
  
  <p>I'm trying to create nine <code>threads</code>, and have them all run at the same time to figure out which is the most efficient.</p>
  
  <pre><code>    int threadNumber = 0;
      pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
      pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  
      #define NUM_THREADS    9
  
      //used to store the information of each thread
      typedef struct{
          pthread_t  threadID;
          int policy;
          struct sched_param param;
          long startTime;
          long taskStartTime;
          long endTime1;
          long endTime2;
          long endTime3;
          long runTime;
          char startDate[30];
          char endDate[30];
      }ThreadInfo;
  
  ThreadInfo myThreadInfo[NUM_THREADS];
  
  
  
  //main function
  int main(void){
  
     printf("running...\n");
  
     pthread_mutex_lock(&amp;mutex); //lock the mutex//////
     pthread_cond_wait(&amp;cond, &amp;mutex); //start waiting//////
  
  
     int fifoPri = 60;
     int rrPri = 30;
  
     //create the 9 threads and assign their scheduling policies
     for(int i=0; i&lt;NUM_THREADS; i++){
  
        if(i%3 == SCHED_OTHER){
              myThreadInfo[i].policy = SCHED_OTHER;
              myThreadInfo[i].param.sched_priority = 0;
  
        }
        else if (i%3 == SCHED_RR){ 
              myThreadInfo[i].policy = SCHED_RR;
              myThreadInfo[i].param.sched_priority = rrPri++; 
        }
  
        else{
              myThreadInfo[i].policy = SCHED_FIFO; 
              myThreadInfo[i].param.sched_priority = fifoPri++; 
  
        }
  
        pthread_create( &amp;myThreadInfo[i].threadID, NULL, ThreadRunner, &amp;myThreadInfo[i]);
  
     }
  
     printf("\n\n");
  
     pthread_mutex_unlock(&amp;mutex) //unlock the mutex/////////
     pthread_cond_signal(&amp;cond); //signal the threads to start////////
  
  
     //join each thread
     for(int g = 0; g &lt; NUM_THREADS; g++){
        pthread_join(myThreadInfo[g].threadID, NULL);
     }
  
  
     //print out the stats for each thread and perform an analysis of the data
     DisplayThreadSchdStats();
  
  
     return 0;
  }
  
  ...
  </code></pre>
  
  <p>so when the main function starts, I <code>lock</code> the mutex to make sure the threads don't start before I tell them to with  pthread_lock(&amp;mutex) and pthread_cond_wait(&amp;cond, &amp;mutex)</p>
  
  <p>then I create all nine threads with various scheduling policies. After that's completely done, I try to tell the threads to all start at the same time using pthread_mutex_unlock(&amp;mutex) and pthread_cond_signal(&amp;cond)</p>
  
  <p>But when I run this, it never unlocks the threads. The main function's "running..." print statement goes off, but the threads never start. (threadrunner has a function where they all print out a ton of different numbers so I can see if they launch). What am I doing wrong with the pthread mutex and pthread cond?</p>