Boost库之circular_buffer
Boost库之circular_buffer
原文:http://blog.csdn.net/byxdaz/article/details/71631470
Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::list或std::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;
- }
Boost库之circular_buffer
版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/byxdaz/article/details/71631470
Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据。它是一个与STL兼容的容器,类似于 std::list或std::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;
- }
Boost库之circular_buffer相关推荐
- C++ Boost库:简介和第一个示例程序
文章目录 1. 简介 2. Boost库开发环境搭建 3. 一个简单的示例程序 4. hpp文件简介 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cas ...
- C++ Boost库:数值转换 lexical_cast
文章目录 1. C/C++数值转换函数 2. boost数值转换lexical_cast C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cast C++ ...
- C++ Boost库:日期时间库 date_time
文章目录 1. 简介 2. 基本使用 3. date_period类 4. ptime类 5. time_period类 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 l ...
- android平台移植boost库
android平台移植boost库 在Android平台使用boost库,没有linux平台上那么方便. 尤其在新版本ndk,高通的车机项目,网上的boost_for_android不一定适用. 本人 ...
- C++ Boost库:windows下编译Boost库
文章目录 1. 需要编译的boost库 2. 编译步骤 3. 库命名规则 4. VS配置库目录 C++ Boost库:简介和第一个示例程序 C++ Boost库:数值转换 lexical_cast C ...
- Mysql依赖库Boost的源码安装,linux下boost库的安装
boost'准标准库'安装过程. 安装的是boost_1_60_0. (1)首先去下载最新的boost代码包,网址www.boost.org. (2)进入到自己的目录,解压: bzip2 -d boo ...
- 简单解析C++基于Boost库实现命令行
Boost库中默认自带了一个功能强大的命令行参数解析器,以往我都是自己实现参数解析的,今天偶尔发现这个好东西,就来总结一下参数解析的基本用法,该库需要引入program_options.hpp头文件, ...
- C++ boost库安装
------------Windows------------------ Boost.Asio--(1)安装及配置 ------------ubuntu------------------ boos ...
- linux C++安装并编译boost库
编译安装 wget https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz tar -zxvf b ...
最新文章
- 谷歌搜索喜迎20年,为手机主页添加信息流!
- 微软 Surface Pro、Studio、Laptop 全线更新
- oracle设置表字段小写,将oracle中的字段和表名全部修改为小写
- 如何计算一年总共有多少周_余额宝1万一天收益多少?如何计算?
- HNOI2017 day1 T3 礼物
- 模拟器上安装不能被卸载的apk
- c++基础学习(11)--(模板、预处理器、信号处理)
- scrapy 伪装代理和 fake_userAgent 的使用
- 同程旅行王晓波:同程凤凰缓存系统在基于 Redis 方面的设计与实践(上篇)
- python断点调试出现问题_Python错误、调试
- 照片转3d模型_三星使用AI将照片转换为3D模型
- 用python画玫瑰花-用python画一朵玫瑰给你
- 敏捷个人学习----为什么的力量
- ssl证书链的验证的其它方式
- SONiC:为Microsoft全球云提供支持的网络交换机软件
- 第一章 时间序列基础知识
- Arcgis空间自相关Moran I(莫兰指数)
- ac数论之矩阵的平方和
- man fgetc fgets getc getchar ungetc
- vs2010环境下提示找不到d3dx9.h,及其“dxerr.lib”。
热门文章
- 安卓安装mysql数据库文件_android安装mysql数据库
- sql 服务器实例怎样显示,如何查看sql数据库的服务器名
- NeHe教程Qt实现——lesson10
- 问题集锦(21-25)
- mysql默认几个库_MySQL 安装初始化mysql后,默认几个库介绍
- 容量法和库仑法的异同点_【图文专辑】第十讲:容量法高锰酸盐指数的测定
- java 内部类泛型,java – 使用泛型强制转换为内部类
- 聚合函数的计算机控件,使用Kendo UI MVC Grid包装器的聚合函数
- 学习笔记 mysql_MySQL 学习笔记
- java服务端同时接收和发送_如何实现客户端接收数据和发送数据的同步?