title: producer consumer (using Monitor) code is not working when sleep() is used?
body: <pre><code>#include&lt;bits/stdc++.h&gt;
  #include&lt;pthread.h&gt;
  #include&lt;unistd.h&gt;
  #define MAX 10
  using namespace std;
  class BoundedBuffer
  {
  private:
    int buffer[MAX];
    int fill, use;
    int fullEntries;
    pthread_mutex_t monitor;  // monitor lock
    pthread_cond_t empty;
    pthread_cond_t full;
  public:
    BoundedBuffer ()
    {
      use = fill = fullEntries = 0;
    }
    void produce (int element)
    {
      pthread_mutex_lock (&amp;monitor);
      while (fullEntries == MAX)
        pthread_cond_wait (&amp;empty, &amp;monitor);
      buffer[fill] = element;
      fill = (fill + 1) % MAX;
      fullEntries++;
      //sleep(rand()%2);
      pthread_cond_signal (&amp;full);
      pthread_mutex_unlock (&amp;monitor);
    }
    int consume ()
    {
      pthread_mutex_lock (&amp;monitor);
      while (fullEntries == 0)
        pthread_cond_wait (&amp;full, &amp;monitor);
      int tmp = buffer[use];
      use = (use + 1) % MAX;
      fullEntries--;
      //sleep(rand()%2);
      pthread_cond_signal (&amp;empty);
      pthread_mutex_unlock (&amp;monitor);
      return tmp;
    }
  }b;
  void* producer(void *arg){
  int i=1;
  while(true){
  b.produce(i);
  i++;
  }
  }
  void* consumer(void *arg){
  while(true){
  cout&lt;&lt;b.consume()&lt;&lt;" ";
  }
  }
  int main(){
  pthread_t t1,t2;
  pthread_create(&amp;t1,NULL,producer,NULL);
  pthread_create(&amp;t2,NULL,consumer,NULL);
  pthread_join(t1,NULL);
  pthread_join(t2,NULL);
  return 0;
  }
  </code></pre>
  
  <p>Whenever sleep() is added  in BoundedBuffer.consume() and BoundedBuffer.produce(int ) then it doesn't print any output.But when there is no sleep() in these functions then it works fine and print output as it should be .Why it's happening ?<br/>
  <strong>Reference:</strong>
  <a href="http://pages.cs.wisc.edu/~remzi/OSTEP/threads-monitors.pdf" rel="nofollow">http://pages.cs.wisc.edu/~remzi/OSTEP/threads-monitors.pdf</a>   </p>