template <class T, class ARG_T>
class CQueue
{
public:
CQueue()
{
m_pHead = NULL;
m_pTail = NULL;
m_nCount = 0 ;
m_nIndex = 0 ;
m_bAutoDeleElement = FALSE ;
InitializeCriticalSection(&hCriticalSection);
}

~CQueue()
{
Clear();
DeleteCriticalSection(&hCriticalSection);
}
public:
int Append(ARG_T &element)//这里写引用还是 arg_t element ?
{
EnterCriticalSection(&hCriticalSection);
CElement *p = new CElement;
if ( p )
{
p->Data = element ;//参数是引用
if ( m_nCount )
{
p->Next = NULL;
p->Prev = m_pTail;
m_pTail->Next = p;
m_pTail = p;
}
else 
{
m_pHead = p;
m_pTail = p;
}
}
LeaveCriticalSection(&hCriticalSection);
return ++m_nCount;
} T &GetHead()
{
ASSERT( m_pHead != NULL);
EnterCriticalSection(&hCriticalSection);
CElement *p = m_pHead ;
T &t = p->Data;
if ( m_nCount )
{
m_pHead = m_pHead->Next ;
if ( m_pHead )
m_pHead->Prev = NULL ;
p->Next = NULL;
p->Prev = NULL;
delete p ;//这里删除P后, p->data也不存在了,我要返回其值,该怎么?
m_nCount -- ;
}
LeaveCriticalSection(&hCriticalSection);
return t;
}

void Clear()
{
EnterCriticalSection(&hCriticalSection);
CElement *p = m_pHead;
CElement *q = NULL;
while ( m_nCount && p)
{
q = p->Next ;
p->Data.~T();
delete p;
p = q ;
m_nCount -- ;
}
LeaveCriticalSection(&hCriticalSection);
} int GetCount()
{
return m_nCount;
} public:
struct CElement
{
CElement * Prev;
CElement * Next;
CElement()
{
Prev = NULL;
Next = NULL;
Data = NULL;
};
T Data;
};
private:
int m_nIndex;
int m_nCount;
BOOL m_bAutoDeleElement;

CElement *m_pHead;
CElement *m_pTail;

CRITICAL_SECTION hCriticalSection ;
};