码迷,mamicode.com
首页 > 其他好文 > 详细

无锁的环形队列

时间:2015-01-11 14:43:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

#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 == 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 == 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

 

无锁的环形队列

标签:

原文地址:http://www.cnblogs.com/zzyoucan/p/4216454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!