1. 程式人生 > >Boost庫之circular_buffer

Boost庫之circular_buffer

         Boost.Circular_buffer維護了一塊連續記憶體塊作為快取區,當快取區內的資料存滿時,繼續存入資料就覆蓋掉舊的資料。Boost.Circular_buffer維護了一塊連續記憶體塊作為快取區,當快取區內的資料存滿時,繼續存入資料就覆蓋掉舊的資料。它是一個與STL相容的容器,類似於 std::liststd::deque,並且支援隨機存取。circular_buffer被特別設計為提供固定容量的儲存大小。當其容量被用完時,新插入的元素會覆蓋緩衝區頭部或尾部(取決於使用何種插入操作)的元素。

#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <boost/circular_buffer.hpp>
typedef unsigned int size_type;

template<typename T>
class SafeCircularBuffer
{
public:
	SafeCircularBuffer(int size) :
	  buffer_(size)
	  {
	  };
	  size_type capacity()
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.capacity();
	  };
	  void set_capacity(size_type new_capacity)
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  buffer_.set_capacity(new_capacity);
	  };
	  size_type size() const
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.size();
	  };
	  bool push_back(const T& item)
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  if (buffer_.capacity() == 0)
			  return false;

		  if (!buffer_.full())
		  {
			  buffer_.push_back(item);
			  return true;
		  }

		  return false;
	  };
	  bool push_front(const T& item)
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  if (buffer_.capacity() == 0)
			  return false;
		  buffer_.push_front(item);
		  return true;
	  };
	  void clear()
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  buffer_.clear();
	  };
	  T& front()
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.front();
	  };
	  void pop_front()
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  buffer_.pop_front();
	  };
	  void pop_back()
	  {
		  boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
		  buffer_.pop_back();
	  };
	  unsigned int size()
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.size();
	  };
	  bool empty()
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.empty();
	  };
	  bool full()
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_.full();
	  };
	  T& at(size_t index)
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  try
		  {
			  buffer_.at(index);
		  }
		  catch(std::out_of_range& ex)
		  {
			  throw(ex);
		  }
		  return buffer_[index];
	  };
	  T& operator[](size_t index)
	  {
		  boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
		  return buffer_[index];
	  };
private:
	boost::circular_buffer<T> buffer_;
	boost::shared_mutex  buffer_mutex_;			//讀寫鎖
};

#include <boost/circular_buffer.hpp>
#include <iostream>
using namespace std;

int main()
{
	//非執行緒安全circular_buffer
	typedef boost::circular_buffer<int> circular_buffer;
	circular_buffer cb(3);
	cout << cb.capacity() <<endl;
	cout << cb.size() <<endl;

	cb.push_back(0);
	cb.push_back(1);
	cb.push_back(2);
	cout << cb.size() <<endl;

	//設定新容量大小
	cb.set_capacity(6);
	cb.push_back(3);
	cb.push_back(4);
	cb.push_back(5);
	cout << cb.size() << endl;
	
	//資料迴圈(從某個位置開始)
	for(int i=0; i<cb.size(); i++)
	{
		cout << cb[i];
		if(i != cb.size()-1)
		{
			cout << ",";
		}
	}
	cout<<endl;
	cb.rotate(cb.begin()+1);
	for(int i=0; i<cb.size(); i++)
	{
		cout << cb[i];
		if(i != cb.size()-1)
		{
			cout << ",";
		}
	}
	cout<<endl;

	//執行緒安全circular_buffer
	SafeCircularBuffer<double> scbCircularBuffer(3);
	cout << scbCircularBuffer.capacity() <<endl;
	cout << scbCircularBuffer.size() <<endl;

	scbCircularBuffer.push_back(1.0999f);
	scbCircularBuffer.push_back(1.1f);
	scbCircularBuffer.push_back(2.2f);

	cout << scbCircularBuffer.size() <<endl;
	scbCircularBuffer.push_back(3.3f);
	cout << scbCircularBuffer[0]<<endl;
	int k = 0;
}