title: Understanding pthread
body: <p>I'm currently confused as to why the following code won't print the following:</p>
  
  <pre><code>My value is 0
  My value is 1
  My value is 2
  </code></pre>
  
  <p>Every time I run this I either get 1-2 printed lines or nothing and the program just sits their until I ctrl-c. I feel like it might have something to do with me using the same condition variable and mutex with 3 different threads, would this be correct? Any explanations are greatly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct id_holder
  {
   int id;
  };
  
  pthread_mutex_t intersectionMutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t directionCondition = PTHREAD_COND_INITIALIZER;
  struct id_holder * holder;
  
  void * logic(void* val)
  {
    struct id_holder * id_struct = (struct id_holder *) val;
  
    pthread_cond_wait(&amp;directionCondition, &amp;intersectionMutex);
    printf("My value is %d\n", id_struct-&gt;id);
    free(id_struct);
    return NULL;
  }
  
  int main(void)
  {
       pthread_t threads[3];
  
       for(int i = 0; i &lt;3; i++)
       {
         holder = (struct id_holder *) malloc(sizeof(struct id_holder));
         holder-&gt;id = i;
         pthread_create(&amp;threads[i], NULL, logic, holder);
       }
  
       for(int i = 0; i &lt; 3; i++)
       {
         sleep(1);
         pthread_cond_signal(&amp;directionCondition);
       }
  
       for(int i = 0; i &lt; 3; i++)
       {
         pthread_join(threads[i], NULL);
       }
  
       return 0;
  }
  </code></pre>