title: How to use mutexes in C
body: <p>I am confused about use of multiple mutexes in C. </p>
  
  <pre><code>int main() {
  
          pthread_t thread1;
          char *message1 = "Thread 1";
          int  r;
          pthread_mutex_init(&amp;mutex1, NULL);
          pthread_mutex_init(&amp;mutex2, NULL);
  
          pthread_mutex_lock(&amp;mutex1);
  
          r = pthread_create( &amp;thread1, NULL, print_message_function, (void*) message1);
  
          printf("Parent 1\n");
  
  
          pthread_mutex_lock(&amp;mutex2);
          printf("Parent 2\n");
          pthread_mutex_unlock(&amp;mutex2);
  
  
          pthread_mutex_unlock(&amp;mutex1);
  
  
          pthread_join( thread1, NULL);
  
          printf("Thread 1 returns: %d\n",r);
          return 0;
  }
  
  void *print_message_function( void *str ) {
  
          pthread_mutex_lock(&amp;mutex1);
          char *message;
          message = (char *) str;
          printf("Child 1 received message: %s \n", message);
  
  
          pthread_mutex_lock(&amp;mutex2);
          printf("child 2\n");
          pthread_mutex_unlock(&amp;mutex2);
  
  
  
  
          pthread_mutex_unlock(&amp;mutex1);
  
          return NULL;
  }
  </code></pre>
  
  <p>output is</p>
  
  <pre><code>Parent 1
  Parent 2
  Child 1 received message: Thread 1 
  child 2
  Thread 1 returns: 0
  </code></pre>
  
  <p>what i want is</p>
  
  <pre><code>Parent 1
  Child 1 received message: Thread 1 
  Parent 2
  child 2
  Thread 1 returns: 0
  </code></pre>