title: Thread safe implementation of circular buffer
body: <p>Circular_buffer from boost library is not thread-safe. So I wrapped boost::circular_buffer object in a class as shown below. Mutual exclusion between the threads is achieved (I think) by using conditional variables, a mutex and a lock acquisition/release. Is this implementation thread safe? </p>
  
  <pre><code>#include &lt;boost/thread/condition.hpp&gt;
  #include &lt;boost/thread/mutex.hpp&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  #include &lt;boost/circular_buffer.hpp&gt;
  
  // Thread safe circular buffer 
  template &lt;typename T&gt;
  class circ_buffer : private boost::noncopyable
  {
  public:
      typedef boost::mutex::scoped_lock lock;
      circ_buffer() {}
      circ_buffer(int n) {cb.set_capacity(n);}
      void send (T imdata) {
          lock lk(monitor);
          cb.push_back(imdata);
          buffer_not_empty.notify_one();
      }
      T receive() {
          lock lk(monitor);
          while (cb.empty())
              buffer_not_empty.wait(lk);
          T imdata = cb.front();
          cb.pop_front();
          return imdata;
      }
      void clear() {
          lock lk(monitor);
          cb.clear();
      }
      int size() {
          lock lk(monitor);
          return cb.size();
      }
      void set_capacity(int capacity) {
          lock lk(monitor);
          cb.set_capacity(capacity);
      }
  private:
      boost::condition buffer_not_empty;
      boost::mutex monitor;
      boost::circular_buffer&lt;T&gt; cb;
  };
  </code></pre>
  
  <p><strong>Edit</strong>  This is now a template class, which accepts an object of any type (not just <code>cv::Mat</code> object). </p>