title: Try to compare 2 methods to implement bounded blocking queue
body: <p>bounded blocking queue is famous, of course. There are mostly 2 methods to implement it. I try to understand which way is better:
  Method 1: use counting semaphore</p>
  
  <pre><code>void *producer(void *arg) {
  int i;
  for (i = 0; i &lt; loops; i++) {
      sem_wait(&amp;empty);           
      sem_wait(&amp;mutex);           
      put(i);                     
      sem_post(&amp;mutex);           
      sem_post(&amp;full);            
  }
  }
  void *consumer(void *arg) {
      int i;
      for (i = 0; i &lt; loops; i++) {
          sem_wait(&amp;full);            
          sem_wait(&amp;mutex);           
          int tmp = get();            
          sem_post(&amp;mutex);           
          sem_post(&amp;empty);           
          printf("%d\n", tmp);
      }
  }
  </code></pre>
  
  <p>Method 2: classic monitor pattern</p>
  
  <pre><code>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); 
          //do something
          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);           
          //do something
          pthread_cond_signal(&amp;empty);            
          pthread_mutex_unlock(&amp;monitor);
          return tmp;                
      }
  }
  </code></pre>
  
  <p>I understand the 2nd method can solve a lot of other problems. But how to compare these 2 methods? Looks like they can both fulfill the task. 
  Is there any link on detailed comparision?
  Appreciate your help. 
  Thanks. </p>