循环队列 作为消息队列
2016-06-12 20:21
302 查看
为了避免消息队列频繁的申请和释放内存,采用循环队列作为消息队列。
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 1024
class 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;
}
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 1024
class 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;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- mongo实现消息队列
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解