title: Will a thread waiting on a mutex get the ownership, immediately after mutex_unlock() by other thread?
body: <p>I have two threads - threadA &amp; threadB. If B is waiting for mutex, which is owned by A, Will it get the ownership immediately after A unlocks it, assuming it has higher priority than A ?<br>
  This not a question on who gets the lock when multiple threads are waiting, but if a single waiting thread becomes runnable &amp; gets the processor or not.</p>
  
  <p>From the test example below it doesn't seem to happen always. Can someone please clarify ?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;pthread.h&gt;
  
  //global variables
  /* START = THREAD A runs, STOP = THREAD A Stops &amp; Waits */
  volatile enum { START, STOP } state = START;
  pthread_cond_t      condA  = PTHREAD_COND_INITIALIZER;
  pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
  
  void *threadA()
  {
  int i = 0;
  int j = 0;
  
  struct sched_param p;
  p.sched_priority                        =       16;
  pthread_setschedparam(pthread_self(), SCHED_FIFO, &amp;p);
  
  printf("threadA created\n");
  while(1)
  {
      printf("threadA lock requested  \n");
      pthread_mutex_lock(&amp;mutex);
      while (state == STOP)
      {
          printf("Waiting in STOP state, until some one sends START again\n");
          pthread_cond_wait(&amp;condA, &amp;mutex);
      }
      printf("threadA locked mutex  \n");
      //do stuff - ~a few ms of work, simulated with dummy for loop
      for(j=0; j&lt;=100000000; j++)
          ;
      i++;
      printf("threadA loop cntr %d\n",i);
      //printf("threadA   unlock requested  \n");
      pthread_mutex_unlock(&amp;mutex);
      printf("threadA unlocked mutex  \n");
  }
  fflush(stdout);
  return 0;
  }
  
  void *threadB()
  {
  
  struct sched_param p;
  p.sched_priority                        =       17;
  pthread_setschedparam(pthread_self(), SCHED_FIFO, &amp;p);
  
  printf("threadB created\n");
  do
  {
      /* Time to stop threadA */
      printf("threadB lock requested  \n");
      pthread_mutex_lock(&amp;mutex);
      printf("threadB locked mutex  \n");
      state = STOP;
      pthread_cond_signal(&amp;condA);
      //printf("threadB unlock requested  \n");
      pthread_mutex_unlock(&amp;mutex);
      //printf("threadB unlocked mutex  \n");
  }while(0);
  fflush(stdout);
  return 0;
  }
  
  int main(int argc, char *argv[])
  {
  int j = 0;
  
  //create our threads
  pthread_t a, b;
  
  pthread_create(&amp;a, NULL, threadA, NULL);
  /* Wait for a while to make sure A is 
     up &amp; running before stopping it */
  for(j=0; j&lt;=100000; j++)
      ;
  // Now stop A
  pthread_create(&amp;b, NULL, threadB, NULL);
  
  fflush(stdout);
  
  
  pthread_join(a, NULL);
  pthread_join(b,NULL);
  }
  </code></pre>
  
  <p>Typical output I see is as below..<br>
  threadA created<br>
  threadA lock requested<br>
  threadA locked mutex<br>
  threadB created<br>
  threadB lock requested<br>
  threadA loop cntr 1<br>
  threadA unlocked mutex &lt;&lt; <em>A unlocked it, so a waiting B should receive it here??</em><br>
  threadA lock requested<br>
  threadA locked mutex<br>
  threadA loop cntr 2<br>
  threadB locked mutex  &lt;&lt; <em>B granted ownership finally !!</em><br>
  threadA unlocked mutex<br>
  threadA lock requested<br>
  Waiting in STOP state, until some one sends START again  </p>