/* * CircularBufferMT.hh * * This class contains a data member of circular buffer type and makes sure that the * there is no problem reading and writing to the buffer from multiple threads. * It also protects against popping elements from empty buffer that causes runtime errors. * Not all methods of circular_buffer are implemented, they are implemented on a need-to-be basis. * * Created on: Dec 15, 2017 * Author: Hovanes Egiyan */ #ifndef CIRCULARBUFFERMT_HH_ #define CIRCULARBUFFERMT_HH_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // for auto_cpu_timer namespace VETROC { template class CircularBufferMT { public: typedef boost::circular_buffer container_type; typedef typename boost::circular_buffer::iterator iterator_type; typedef typename container_type::size_type size_type; typedef typename container_type::value_type value_type; typedef typename boost::call_traits::param_type param_type; private: boost::mutex m_mutex; container_type m_container; boost::condition m_not_empty; CircularBufferMT( const CircularBufferMT& ); // Disabled copy constructor. CircularBufferMT& operator =( const CircularBufferMT& ); // Disabled assign operator. public: CircularBufferMT( size_type capacity ) : m_mutex(), m_container( capacity ) { m_container.push_front( value_type() ); } virtual ~CircularBufferMT() { } void pushFront( typename boost::call_traits::param_type item ) { // `param_type` represents the "best" way to pass a parameter of type `value_type` to a method. boost::mutex::scoped_lock lock( m_mutex ); m_container.push_front( item ); lock.unlock(); m_not_empty.notify_one(); } void popBack( value_type* pItem ) { boost::mutex::scoped_lock lock( m_mutex ); m_not_empty.wait( lock, boost::bind( &CircularBufferMT::is_not_empty, this ) ); *pItem = m_container.back(); m_container.pop_back(); } value_type operator[]( unsigned i ) { return m_container[i]; } bool is_not_empty() const { return (!m_container.empty()); } bool is_not_full() const { return (!m_container.full()); } size_type getSize() const { return m_container.size(); } size_type getNumberOfFreeSlots() { return m_container.capacity() - m_container.size(); } }; } /* namespace VETROC */ #endif /* CIRCULARBUFFERMT_HH_ */