首页 > 代码库 > 无锁的环形队列
无锁的环形队列
#ifndef _RingQueue_H_#define _RingQueue_H_#include <memory.h>template<class T, unsigned int MAX_LEN = 1024>class RingQueue{public: //----------------------------------------------------- // 构造 //----------------------------------------------------- RingQueue() { m_nHead = 0; m_nTail = 0; } //----------------------------------------------------- // 析构 //----------------------------------------------------- ~RingQueue() { } //----------------------------------------------------- // 重置,清空队列 //----------------------------------------------------- void Reset() { m_nHead = 0; m_nTail = 0; } //----------------------------------------------------- // 取得最多读取个数 //----------------------------------------------------- unsigned int GetMaxReadSize() { int size = (m_nTail - m_nHead + MAX_LEN) % MAX_LEN; return size; } //----------------------------------------------------- // 取得最多写入个数 //----------------------------------------------------- unsigned int GetMaxWriteSize() { int size = (m_nHead - m_nTail + MAX_LEN - 1) % MAX_LEN; return size; } //----------------------------------------------------- // 添加数据 //----------------------------------------------------- bool PushData(const T* pData, unsigned int nLength = 1) { if (pData =http://www.mamicode.com/= nullptr || nLength > GetMaxWriteSize()) { return false; } if (m_nTail + nLength <= MAX_LEN) { memcpy(m_Buffer + m_nTail, pData, nLength * sizeof(T)); m_nTail = (m_nTail + nLength) % MAX_LEN; } else { unsigned int size1 = MAX_LEN - m_nTail; unsigned int size2 = nLength - size1; memcpy(m_Buffer + m_nTail, pData, size1 * sizeof(T)); memcpy(m_Buffer, pData+size1, size2 * sizeof(T)); m_nTail = size2; } return true; } //----------------------------------------------------- // 取出并删除数据 //----------------------------------------------------- bool PopData(T* pData, unsigned int nLength = 1) { if (pData =http://www.mamicode.com/= nullptr || nLength > GetMaxReadSize() || nLength <= 0) { return false; } if (m_nHead + nLength <= MAX_LEN) { memcpy(pData, m_Buffer + m_nHead, nLength * sizeof(T)); m_nHead = (m_nHead + nLength) % MAX_LEN; } else { unsigned int size1 = MAX_LEN - m_nHead; unsigned int size2 = nLength - size1; memcpy(pData, m_Buffer + m_nHead, size1 * sizeof(T)); memcpy(pData + size1, m_Buffer, size2 * sizeof(T)); m_nHead = size2; } return true; } //----------------------------------------------------- // 查看数据 不删除 //----------------------------------------------------- bool PeekData(T* pData, unsigned int nLen) { if (nLen > GetMaxReadSize() || nLen <= 0) { return false; } if (m_nHead + nLen <= MAX_LEN) { memcpy(pData, m_Buffer + m_nHead, nLen * sizeof(T)); } else { unsigned int size1 = MAX_LEN - m_nHead; unsigned int size2 = nLen - size1; memcpy(pData, m_Buffer + m_nHead, size1 * sizeof(T)); memcpy(pData + size1, m_Buffer, size2 * sizeof(T)); } return true; } //----------------------------------------------------- // 删除数据 //----------------------------------------------------- bool DeleteData(unsigned int nLen) { if (nLen > GetMaxReadSize() || nLen <= 0) { return false; } if (m_nHead + nLen <= MAX_LEN ) { m_nHead = (m_nHead + nLen) % MAX_LEN; } else { m_nHead = nLen - (MAX_LEN - m_nHead); } return true; }public: T m_Buffer[MAX_LEN]; // 数据区private: unsigned int m_nHead; // 队头 unsigned int m_nTail; // 队尾};#endif
无锁的环形队列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。