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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byxdaz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值