C对列队列,先进先出。大神看下有毛病没。
注意:定义MAXQUEUE 20字节,只能存储19字节的内容。有一字节空,是判断队列是否满的标志。
#define MAXQUEUE 20
typedef struct tagMsgQueue
{
int uFront,uRear;
int m_Msg[MAXQUEUE];
}MSGQUEUE;
#define Q_FULL(m) ((((m)->uRear + 1) % MAXQUEUE) == (m)->uFron)
#define Q_EMPTY(m) ((m)->uRear == (m)->uFront )
#define Q_INIT(m) (m)->uFront = 0; (m)->uRear = 0;
bool EnQueue(MSGQUEUE *pMsg, int msg)
{
//int nIntEn; //TODO:Need add enter critical sections codes
if (!Q_FULL(pMsg))
{
//nIntEn = (pMsg->uRear++) % MAXQUEUE;
//pMsg->m_Msg[nIntEn] = msg;
pMsg->m_Msg[pMsg->uRear++] = msg;
pMsg->uRear = pMsg->uRear % MAXQUEUE;
return true;
}
return false;
}
bool DeQueue(MSGQUEUE *pMsg, int *pDeMsg)
{ if (!Q_EMPTY(pMsg))
{
*pDeMsg = pMsg->m_Msg[pMsg->uFront];
pMsg->uFront = (pMsg->uFront+1)%MAXQUEUE;
return true;
}
else
{
Q_INIT(pMsg);
return false;
}
}