title: Mutex and order of execution
body: <p>First of all I am still new to posix programming and still understanding basic concepts.  It is still not very clear for me how do <code>pthread_mutex_lock</code>
  <code>pthread_mutex_unlock</code> do work.</p>
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #inlcude &lt;stdio.h&gt;
  pthread_mutex_t_mtx;
  void* routine(void* i){
      int j;
      for(j = 0; j &lt; 1000000; ++j){
           pthread_mutex_lock(&amp;mtx);
           printf(&quot;Inside thread %d\n&quot;, i);
           pthread_mutex_unlock(&amp;mtx);
      }
      return NULL;
  }
  
  int main()
  {
     pthread_t th[3];
     int i;
     pthread_mutex_init(&amp;mtx, NULL);
     for(i = 1; i &lt;= 2; ++i)
     {
        if(pthread_create(th + i, NULL, routine, i){
             perror(NULL);  return 1;
        }
     }
  
     for(i = 1; i &lt;=2; ++i)
          pthread_join(th[i], NULL);
  
     return 0;
  }
  </code></pre>
  <p>What should be the correct output of the above program ? I think that because of the lock and unlock of the mutex there would be 2000000 iterations, but it is not very clear for me the order that they are done in. Does the first thread execute the first 1000000 steps of the for? Does it even execute the first one of the 20000000 ? Or does this happen by a more chaotic order?</p>