title: Implementing producers/consumers using mutex
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  #define WORK_SIZE 1024
  pthread_mutex_t work_mutex;
  char work_area[WORK_SIZE];
  void *thread_start(void *);
  int main() {
  pthread_t a_thread;
  pthread_mutex_init(&amp;work_mutex,NULL);
  pthread_create(&amp;a_thread,NULL,thread_start,NULL);
  while(1)
  {
  pthread_mutex_lock(&amp;work_mutex);
  printf("Enter some text\n");
  fgets(work_area, WORK_SIZE, stdin);
  pthread_mutex_unlock(&amp;work_mutex);
  }
  return 0;
  }
  
  void *thread_start(void *arg)
  {
  sleep(1);
  while(1)
  {
  pthread_mutex_lock(&amp;work_mutex);
  printf("You enetered %d char",strlen(work_area));
  pthread_mutex_unlock(&amp;work_mutex);
  }
  }
  </code></pre>
  
  <p>When I execute the program, after releasing of the mutex lock in main thread, it again aquires the lock, everytime, before the second thread could aquire the lock. I was expecting that once the main thread would release the lock, the second thread which is already blocked would aquire the lock and start execution before the main.</p>
  
  <p>To be more clear, I am getting such type of output :-</p>
  
  <pre><code>Enter some text
  qwerty
  Enter some text
  asdaf
  Enter some text
  jkdf 
  Enter some text
  </code></pre>