循环队列 作为消息队列
来源:互联网 发布:ipowerl手指充电软件 编辑:程序博客网 时间:2024/04/28 19:39
为了避免消息队列频繁的申请和释放内存,采用循环队列作为消息队列。
queue.h:
#ifndef INC_QUEUE_H_#define INC_QUEUE_H_#include <pthread.h>#include <iostream>using namespace std;#define MAX_QUEUE_SIZE 500000#define SINGLE_DATA_SIZE 1024class Queue{public: Queue(int maxSize = MAX_QUEUE_SIZE, int singleDataSize = SINGLE_DATA_SIZE); ~Queue();public: static Queue* getInstance(); string pop(); bool enqueue(const string& str); bool dequeue(string& str); bool isEmpty()const; bool isFull()const;private: int m_maxSize; string* m_pBase;//内存的使用地址 int m_front; //第一个元素的. 出队列时要取的元素 int m_rear;//最后一个元素的下一个元素 static Queue* m_queue;};#endif /* INC_QUEUE_H_ */
queue.cpp
#include "queue.h"Queue* Queue::m_queue = NULL;Queue::Queue(int maxSize, int bufSize){ m_maxSize = maxSize; m_pBase = new string[maxSize * bufSize]; //to do 申请失败 m_front = 0; m_rear = 0;}Queue::~Queue(){ if(m_pBase) delete []m_pBase;}Queue* Queue::getInstance(){ if(!Queue::m_queue) { m_queue = new Queue(); return m_queue; } return m_queue;}//循环队列保留一个不可用的节点bool Queue::isFull()const{ return m_front == (m_rear + 1)%m_maxSize;}bool Queue::isEmpty()const{ return m_front == m_rear;}string Queue::pop(){ return m_pBase[m_front];}bool Queue::enqueue(const string& str){ if(isFull()) return false; m_pBase[m_rear] = const_cast<char*>(str.c_str()); m_rear = (m_rear + 1)%m_maxSize; return true;}bool Queue::dequeue(string& str){ if(isEmpty()) return false; str = m_pBase[m_front]; m_front = (m_front + 1)%m_maxSize; return true;}
0 0
- 循环队列 作为消息队列
- 循环消息队列实现
- 循环消息队列
- 消息队列与消息循环
- 消息的循环队列实现
- Windows的消息队列与消息循环
- windows的消息队列与消息循环
- Windows的消息队列与消息循环
- Windows的消息队列与消息循环
- 为什么学习Redis作为消息队列服务器
- redis作为消息队列的使用
- redis作为消息队列实现代码
- php使用redis作为消息队列
- redis作为消息队列的使用
- [示例] -- redis作为消息队列实现代码
- 使用一个消息队列作为计数信号量
- 为什么使用ZeroMQ作为消息队列
- php使用redis作为消息队列
- SQLsever语句建立信息表
- LinuxC/C++编程(10)—socket本地通信
- Java集合细节:subList的缺陷
- win 10 vmware 虚拟网卡无法安装解决方法
- Error connecting to database [No such file or directory]
- 循环队列 作为消息队列
- hdu 3631 Shortest Path【Floyd】
- 【GDOI2014模拟】网格
- 一个简单的从txt文件中读取数据插入数据库
- 对‘pthread_create’未定义的引用 对‘pthread_join’未定义的引用
- 计算机组成原理运算器的编程实现
- 用Kibana和logstash快速搭建实时日志查询、收集与分析系统
- IOS开发支持64位
- God Chang's interview——异或