title: Thread safe queue with fixed size
body: <p>what I want to do is to push integers to my threadSafe queue implementation with multiple threads and concurrently with another series of threads pop away the inserted numbers. All of this operation has to be thread safe, but another option that I want to have is that the size of the queue must be fixed, just like a buffer. If the buffer is full all the push threads must wait the pop threads to free some slot.</p>
  
  <p>This is my implementation of the queue/buffer, it seems to work but after few iterations it stops and remains blocked without any error.</p>
  
  <pre><code>#include &lt;queue&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  #include &lt;iostream&gt;
  
  template &lt;typename T&gt;
  
  class Queue
  {
  private:
      std::queue&lt;T&gt; queue_;
      std::mutex mutex_;
      std::condition_variable cond_;
  
  public:
  
      T pop()
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
  
          cond_.wait(mlock, [this]{return !queue_.empty();});
  
          auto val = queue_.front();
          queue_.pop();
          return val;
      }
  
      void pop(T&amp; item)
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
  
          cond_.wait(mlock, [this]{return !queue_.empty();});
  
          item = queue_.front();
          queue_.pop();
      }
  
      void push(const T&amp; item, int buffer)
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
  
          while (queue_.size() &gt;= buffer)
          {
              cond_.wait(mlock);
          }
  
          queue_.push(item);
          mlock.unlock();
          cond_.notify_one();
      }
  
      Queue()=default;
      Queue(const Queue&amp;) = delete;            // disable copying
      Queue&amp; operator=(const Queue&amp;) = delete; // disable assignment
  
  };
  </code></pre>
  
  <p>The size of the buffer is defined in the push function with the variable buffer. This is an example of usage:</p>
  
  <pre><code> void prepare(Queue&lt;int&gt;&amp; loaded, int buffer, int num_frames)
   {
       for (int i = 0; i &lt; num_frames; i++)
       {
           cout&lt;&lt; "push "&lt;&lt;i&lt;&lt;endl;
           loaded.push(i, buffer);
       }
   }
  
   void load (vector&lt;Frame&gt;&amp; movie, Queue&lt;int&gt;&amp; loaded, int num_frames,
                              int num_points, int buffer, int height, int width)
      {
          for (int i = 0; i &lt; num_frames; i++)
          {
              int num = loaded.pop();
              cout&lt;&lt; "pop "&lt;&lt;num&lt;&lt;endl;
      }
  }
  
  int main()
  {
      srand(time(NULL));
  
      int num_threadsXstage = 4;
  
      int width = 500;
      int height = 500;
  
      int num_points = width * height;
  
      int num_frames = 100;
  
      int frames_thread = num_frames/num_threadsXstage;
  
      int preset = 3;
  
      int buffer = 10;
  
      //Vectors of threads
      vector&lt;thread&gt; loader;
  
      //Final vector
      vector&lt;Frame&gt; movie;
      movie.resize(num_frames);
  
      //Working queues
      Queue&lt;int&gt; loaded;
  
      //Prepare loading queue task
      thread preparator(prepare, ref(loaded), buffer, num_frames);
  
      for (int i = 0; i &lt; num_threadsXstage; i++)
      {
          //stage 1
          loader.push_back(thread(&amp;load, ref(movie), ref(loaded), frames_thread,
                                  num_points, buffer, height, width));
  
      }
  
  
      // JOIN
      preparator.join();
  
      join_all(loader);
  
      return 0;
  }
  </code></pre>