循环队列 作为消息队列详解程序员
2023-06-13 09:20:22 时间
为了避免消息队列频繁的申请和释放内存,采用循环队列作为消息队列。
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;