template<class T,unsigned int ArrayNum=3>//用3个数组,替代锁
class CCircleQueue
{
public:
CCircleQueue()
{
m_nUsePos = 0;
}
切换资源结束
void SwitchSourceEnd()
{
if (m_nUsePos+1 == ArrayNum)
{ //是最后一个下标
m_nUsePos = 0;
}
else
{
++m_nUsePos;
}
}
//切换资源开始
T* SwitchSourceBeg()
{
if (m_nUsePos + 1 == ArrayNum)//2
{
return m_pData;//m_pData[0]
}
else
{
return m_pData + (m_nUsePos + 1)%ArrayNum;//m_pData[1],m_pData[2]
}
}
//获得切换前的资源
T* GetIdleSource()
{
if (m_nUsePos>0)
{
return m_pData+(m_nUsePos-1)%ArrayNum;
}
else
{
return m_pData + ArrayNum - 1; //取最后一个数据
}
}
//获得当前可用资源
T* GetUseSource()
{
return m_pData+m_nUsePos%ArrayNum;
}
void UpdateSource(const T& refSource)
{
*SwitchSourceBeg() = refSource;
SwitchSourceEnd();
}
unsigned int GetQueueSize()
{
return ArrayNum;
}
protected:
unsigned char m_nUsePos;
T m_pData[ArrayNum];
};
class CCircleQueue
{
public:
CCircleQueue()
{
m_nUsePos = 0;
}
切换资源结束
void SwitchSourceEnd()
{
if (m_nUsePos+1 == ArrayNum)
{ //是最后一个下标
m_nUsePos = 0;
}
else
{
++m_nUsePos;
}
}
//切换资源开始
T* SwitchSourceBeg()
{
if (m_nUsePos + 1 == ArrayNum)//2
{
return m_pData;//m_pData[0]
}
else
{
return m_pData + (m_nUsePos + 1)%ArrayNum;//m_pData[1],m_pData[2]
}
}
//获得切换前的资源
T* GetIdleSource()
{
if (m_nUsePos>0)
{
return m_pData+(m_nUsePos-1)%ArrayNum;
}
else
{
return m_pData + ArrayNum - 1; //取最后一个数据
}
}
//获得当前可用资源
T* GetUseSource()
{
return m_pData+m_nUsePos%ArrayNum;
}
void UpdateSource(const T& refSource)
{
*SwitchSourceBeg() = refSource;
SwitchSourceEnd();
}
unsigned int GetQueueSize()
{
return ArrayNum;
}
protected:
unsigned char m_nUsePos;
T m_pData[ArrayNum];
};