Boost库之circular_buffer

原文:http://blog.csdn.net/byxdaz/article/details/71631470

Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::list或std::deque,并且支持随机存取。circular_buffer 被特别设计为提供固定容量的存储大小。当其容量被用完时,新插入的元素会覆盖缓冲区头部或尾部(取决于使用何种插入操作)的元素。

[cpp] view plaincopy
  1. #include <boost/thread/locks.hpp>
  2. #include <boost/thread/shared_mutex.hpp>
  3. #include <boost/circular_buffer.hpp>
  4. typedef unsigned int size_type;
  5. template<typename T>
  6. class SafeCircularBuffer
  7. {
  8. public:
  9. SafeCircularBuffer(int size) :
  10. buffer_(size)
  11. {
  12. };
  13. size_type capacity()
  14. {
  15. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  16. return buffer_.capacity();
  17. };
  18. void set_capacity(size_type new_capacity)
  19. {
  20. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  21. buffer_.set_capacity(new_capacity);
  22. };
  23. size_type size() const
  24. {
  25. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  26. return buffer_.size();
  27. };
  28. bool push_back(const T& item)
  29. {
  30. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  31. if (buffer_.capacity() == 0)
  32. return false;
  33. if (!buffer_.full())
  34. {
  35. buffer_.push_back(item);
  36. return true;
  37. }
  38. return false;
  39. };
  40. bool push_front(const T& item)
  41. {
  42. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  43. if (buffer_.capacity() == 0)
  44. return false;
  45. buffer_.push_front(item);
  46. return true;
  47. };
  48. void clear()
  49. {
  50. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  51. buffer_.clear();
  52. };
  53. T& front()
  54. {
  55. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  56. return buffer_.front();
  57. };
  58. void pop_front()
  59. {
  60. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  61. buffer_.pop_front();
  62. };
  63. void pop_back()
  64. {
  65. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  66. buffer_.pop_back();
  67. };
  68. unsigned int size()
  69. {
  70. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  71. return buffer_.size();
  72. };
  73. bool empty()
  74. {
  75. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  76. return buffer_.empty();
  77. };
  78. bool full()
  79. {
  80. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  81. return buffer_.full();
  82. };
  83. T& at(size_t index)
  84. {
  85. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  86. try
  87. {
  88. buffer_.at(index);
  89. }
  90. catch(std::out_of_range& ex)
  91. {
  92. throw(ex);
  93. }
  94. return buffer_[index];
  95. };
  96. T& operator[](size_t index)
  97. {
  98. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  99. return buffer_[index];
  100. };
  101. private:
  102. boost::circular_buffer<T> buffer_;
  103. boost::shared_mutex  buffer_mutex_;         //读写锁
  104. };
  105. #include <boost/circular_buffer.hpp>
  106. #include <iostream>
  107. using namespace std;
  108. int main()
  109. {
  110. //非线程安全circular_buffer
  111. typedef boost::circular_buffer<int> circular_buffer;
  112. circular_buffer cb(3);
  113. cout << cb.capacity() <<endl;
  114. cout << cb.size() <<endl;
  115. cb.push_back(0);
  116. cb.push_back(1);
  117. cb.push_back(2);
  118. cout << cb.size() <<endl;
  119. //设置新容量大小
  120. cb.set_capacity(6);
  121. cb.push_back(3);
  122. cb.push_back(4);
  123. cb.push_back(5);
  124. cout << cb.size() << endl;
  125. //数据循环(从某个位置开始)
  126. for(int i=0; i<cb.size(); i++)
  127. {
  128. cout << cb[i];
  129. if(i != cb.size()-1)
  130. {
  131. cout << ",";
  132. }
  133. }
  134. cout<<endl;
  135. cb.rotate(cb.begin()+1);
  136. for(int i=0; i<cb.size(); i++)
  137. {
  138. cout << cb[i];
  139. if(i != cb.size()-1)
  140. {
  141. cout << ",";
  142. }
  143. }
  144. cout<<endl;
  145. //线程安全circular_buffer
  146. SafeCircularBuffer<double> scbCircularBuffer(3);
  147. cout << scbCircularBuffer.capacity() <<endl;
  148. cout << scbCircularBuffer.size() <<endl;
  149. scbCircularBuffer.push_back(1.0999f);
  150. scbCircularBuffer.push_back(1.1f);
  151. scbCircularBuffer.push_back(2.2f);
  152. cout << scbCircularBuffer.size() <<endl;
  153. scbCircularBuffer.push_back(3.3f);
  154. cout << scbCircularBuffer[0]<<endl;
  155. int k = 0;
  156. }

Boost库之circular_buffer

2017-05-11 15:13 514人阅读 评论(0) 收藏 举报
 分类:
boost(14) 

版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/byxdaz/article/details/71631470

Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::list或std::deque,并且支持随机存取。circular_buffer 被特别设计为提供固定容量的存储大小。当其容量被用完时,新插入的元素会覆盖缓冲区头部或尾部(取决于使用何种插入操作)的元素。

[cpp] view plaincopy
  1. #include <boost/thread/locks.hpp>
  2. #include <boost/thread/shared_mutex.hpp>
  3. #include <boost/circular_buffer.hpp>
  4. typedef unsigned int size_type;
  5. template<typename T>
  6. class SafeCircularBuffer
  7. {
  8. public:
  9. SafeCircularBuffer(int size) :
  10. buffer_(size)
  11. {
  12. };
  13. size_type capacity()
  14. {
  15. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  16. return buffer_.capacity();
  17. };
  18. void set_capacity(size_type new_capacity)
  19. {
  20. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  21. buffer_.set_capacity(new_capacity);
  22. };
  23. size_type size() const
  24. {
  25. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  26. return buffer_.size();
  27. };
  28. bool push_back(const T& item)
  29. {
  30. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  31. if (buffer_.capacity() == 0)
  32. return false;
  33. if (!buffer_.full())
  34. {
  35. buffer_.push_back(item);
  36. return true;
  37. }
  38. return false;
  39. };
  40. bool push_front(const T& item)
  41. {
  42. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  43. if (buffer_.capacity() == 0)
  44. return false;
  45. buffer_.push_front(item);
  46. return true;
  47. };
  48. void clear()
  49. {
  50. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  51. buffer_.clear();
  52. };
  53. T& front()
  54. {
  55. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  56. return buffer_.front();
  57. };
  58. void pop_front()
  59. {
  60. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  61. buffer_.pop_front();
  62. };
  63. void pop_back()
  64. {
  65. boost::unique_lock< boost::shared_mutex > write_lock(buffer_mutex_);
  66. buffer_.pop_back();
  67. };
  68. unsigned int size()
  69. {
  70. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  71. return buffer_.size();
  72. };
  73. bool empty()
  74. {
  75. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  76. return buffer_.empty();
  77. };
  78. bool full()
  79. {
  80. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  81. return buffer_.full();
  82. };
  83. T& at(size_t index)
  84. {
  85. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  86. try
  87. {
  88. buffer_.at(index);
  89. }
  90. catch(std::out_of_range& ex)
  91. {
  92. throw(ex);
  93. }
  94. return buffer_[index];
  95. };
  96. T& operator[](size_t index)
  97. {
  98. boost::shared_lock< boost::shared_mutex > read_lock(buffer_mutex_);
  99. return buffer_[index];
  100. };
  101. private:
  102. boost::circular_buffer<T> buffer_;
  103. boost::shared_mutex  buffer_mutex_;         //读写锁
  104. };
  105. #include <boost/circular_buffer.hpp>
  106. #include <iostream>
  107. using namespace std;
  108. int main()
  109. {
  110. //非线程安全circular_buffer
  111. typedef boost::circular_buffer<int> circular_buffer;
  112. circular_buffer cb(3);
  113. cout << cb.capacity() <<endl;
  114. cout << cb.size() <<endl;
  115. cb.push_back(0);
  116. cb.push_back(1);
  117. cb.push_back(2);
  118. cout << cb.size() <<endl;
  119. //设置新容量大小
  120. cb.set_capacity(6);
  121. cb.push_back(3);
  122. cb.push_back(4);
  123. cb.push_back(5);
  124. cout << cb.size() << endl;
  125. //数据循环(从某个位置开始)
  126. for(int i=0; i<cb.size(); i++)
  127. {
  128. cout << cb[i];
  129. if(i != cb.size()-1)
  130. {
  131. cout << ",";
  132. }
  133. }
  134. cout<<endl;
  135. cb.rotate(cb.begin()+1);
  136. for(int i=0; i<cb.size(); i++)
  137. {
  138. cout << cb[i];
  139. if(i != cb.size()-1)
  140. {
  141. cout << ",";
  142. }
  143. }
  144. cout<<endl;
  145. //线程安全circular_buffer
  146. SafeCircularBuffer<double> scbCircularBuffer(3);
  147. cout << scbCircularBuffer.capacity() <<endl;
  148. cout << scbCircularBuffer.size() <<endl;
  149. scbCircularBuffer.push_back(1.0999f);
  150. scbCircularBuffer.push_back(1.1f);
  151. scbCircularBuffer.push_back(2.2f);
  152. cout << scbCircularBuffer.size() <<endl;
  153. scbCircularBuffer.push_back(3.3f);
  154. cout << scbCircularBuffer[0]<<endl;
  155. int k = 0;
  156. }

Boost库之circular_buffer相关推荐

  1. C++ Boost库:简介和第一个示例程序

    文章目录 1. 简介 2. Boost库开发环境搭建 3. 一个简单的示例程序 4. hpp文件简介 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cas ...

  2. C++ Boost库:数值转换 lexical_cast

    文章目录 1. C/C++数值转换函数 2. boost数值转换lexical_cast C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cast C++ ...

  3. C++ Boost库:日期时间库 date_time

    文章目录 1. 简介 2. 基本使用 3. date_period类 4. ptime类 5. time_period类 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 l ...

  4. android平台移植boost库

    android平台移植boost库 在Android平台使用boost库,没有linux平台上那么方便. 尤其在新版本ndk,高通的车机项目,网上的boost_for_android不一定适用. 本人 ...

  5. C++ Boost库:windows下编译Boost库

    文章目录 1. 需要编译的boost库 2. 编译步骤 3. 库命名规则 4. VS配置库目录 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cast C ...

  6. Mysql依赖库Boost的源码安装,linux下boost库的安装

    boost'准标准库'安装过程. 安装的是boost_1_60_0. (1)首先去下载最新的boost代码包,网址www.boost.org. (2)进入到自己的目录,解压: bzip2 -d boo ...

  7. 简单解析C++基于Boost库实现命令行

    Boost库中默认自带了一个功能强大的命令行参数解析器,以往我都是自己实现参数解析的,今天偶尔发现这个好东西,就来总结一下参数解析的基本用法,该库需要引入program_options.hpp头文件, ...

  8. C++ boost库安装

    ------------Windows------------------ Boost.Asio--(1)安装及配置 ------------ubuntu------------------ boos ...

  9. linux C++安装并编译boost库

    编译安装 ​​​​ wget https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz tar -zxvf b ...

最新文章

  1. 谷歌搜索喜迎20年,为手机主页添加信息流!
  2. 微软 Surface Pro、Studio、Laptop 全线更新
  3. oracle设置表字段小写,将oracle中的字段和表名全部修改为小写
  4. 如何计算一年总共有多少周_余额宝1万一天收益多少?如何计算?
  5. HNOI2017 day1 T3 礼物
  6. 模拟器上安装不能被卸载的apk
  7. c++基础学习(11)--(模板、预处理器、信号处理)
  8. scrapy 伪装代理和 fake_userAgent 的使用
  9. 同程旅行王晓波:同程凤凰缓存系统在基于 Redis 方面的设计与实践(上篇)
  10. python断点调试出现问题_Python错误、调试
  11. 照片转3d模型_三星使用AI将照片转换为3D模型
  12. 用python画玫瑰花-用python画一朵玫瑰给你
  13. 敏捷个人学习----为什么的力量
  14. ssl证书链的验证的其它方式
  15. SONiC:为Microsoft全球云提供支持的网络交换机软件
  16. 第一章 时间序列基础知识
  17. Arcgis空间自相关Moran I(莫兰指数)
  18. ac数论之矩阵的平方和
  19. man fgetc fgets getc getchar ungetc
  20. vs2010环境下提示找不到d3dx9.h,及其“dxerr.lib”。

热门文章

  1. 安卓安装mysql数据库文件_android安装mysql数据库
  2. sql 服务器实例怎样显示,如何查看sql数据库的服务器名
  3. NeHe教程Qt实现——lesson10
  4. 问题集锦(21-25)
  5. mysql默认几个库_MySQL 安装初始化mysql后,默认几个库介绍
  6. 容量法和库仑法的异同点_【图文专辑】第十讲:容量法高锰酸盐指数的测定
  7. java 内部类泛型,java – 使用泛型强制转换为内部类
  8. 聚合函数的计算机控件,使用Kendo UI MVC Grid包装器的聚合函数
  9. 学习笔记 mysql_MySQL 学习笔记
  10. java服务端同时接收和发送_如何实现客户端接收数据和发送数据的同步?