// ---------------------------------实现文件---------------------------------//
#include "threadsafequeue.h"
CTreadSafeMsgQueue::CTreadSafeMsgQueue(int QueSize, USHORT InvalidMsgType)
{
INVALID_MSG_TYPE = InvalidMsgType;
MAX_QUE_SIZE = QueSize;
m_Queue = NULL;
m_HeaderToWrite = 0;
m_TailToRead = 0;
m_S_Producer = NULL;
m_S_Consumer = NULL;
m_E_Queue = NULL;
m_WritingThreadNum = 0;
m_ReadingThreadNum = 0;
m_bStop = FALSE;
m_bInitedOK = TRUE; // 注意
if (QueSize > 0) m_Queue = new MsgItem [QueSize];
m_bInitedOK &= (m_Queue != NULL);
if (!m_bInitedOK) return;
m_S_Producer = CreateSemaphore(NULL, MAX_QUE_SIZE, MAX_QUE_SIZE, NULL);
m_bInitedOK &= (m_S_Producer != NULL);
if (!m_bInitedOK) return;
m_S_Consumer = CreateSemaphore(NULL, 0, MAX_QUE_SIZE, NULL);
m_bInitedOK &= (m_S_Consumer != NULL);
if (!m_bInitedOK) return;
m_E_Queue = CreateEvent(NULL, FALSE, TRUE, NULL);
m_bInitedOK &= (m_E_Queue != NULL);
}
CTreadSafeMsgQueue::~CTreadSafeMsgQueue()
{
// 防止新的线程进入
m_bInitedOK = FALSE;
// 如果Reset,等待Reset完成
while (m_bStop) Sleep(SLEEP_TIME);
// 等待至少一类线程(读线程或写线程)退出同步状态
while ((m_WritingThreadNum != 0) && (m_ReadingThreadNum != 0)) Sleep(SLEEP_TIME);
// 此时线程必定阻塞于信号量(Semaphore)状态






