📄 msgque.cpp
字号:
//File Name: MsgQue.cpp: implementation of the MsgQue and SafeMsgQue structure.
//References: None
//Purpose: To implement a Safe Message Queue and some essential functions about it.
//History
//*Data *Name *comments.
//2008-Feb-03 Rock Li Initialization
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MsgQue.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//Purpose: To initialize the the pSafeQue message queue
//Arguments description:
//hProExit --> program exit flag
//pSafeQue --> the point to safe message queue
//return value: true for success and false for failure
bool SafeMsgQueInit(HANDLE hProExit, pSafeMsgQue pSafeQue)
{
HANDLE hOpSem = CreateSemaphore(
NULL, // default security attributes
1, // initial count
1, // maximum count
NULL); // unnamed semaphore
if (hOpSem == NULL)
{
//printf("CreateSemaphore error: %d\n", GetLastError());
return false; //failure to Create
}
HANDLE hMsgSem = CreateSemaphore(
NULL, // default security attributes
0, // initial count
1, // maximum count
NULL); // unnamed semaphore
if (hMsgSem == NULL)
{
//printf("CreateSemaphore error: %d\n", GetLastError());
return false; //failure to Create
}
pSafeQue->pMsg = NULL;
pSafeQue->hOpSem = hOpSem;
pSafeQue->hProExitEvn = hProExit;
pSafeQue->hMsgSem = hMsgSem;
pSafeQue->pHead = NULL;
pSafeQue->pTail = NULL;
return true;
}
//Purpose: To Insert a message into the safe message queue
//Arguments description:
//msg --> the message to be inserted into the safe message queue
//pSafeQue --> the pointer to the safe message queue
//return value: true for success and false for failure
//Notice: only one node should be in the Pmsg.
bool InsertOneMsg(pMsgQue pMsg, pSafeMsgQue pSafeQue)
{
if(!LockQue(pSafeQue)) // safe operation; To get the right to use the resurece
{
return false;
}
if(pSafeQue->pHead == NULL)
{
pSafeQue->pHead = pMsg;
pMsg->next =NULL;
}
if(pSafeQue->pTail == NULL)
{
pSafeQue->pTail = pMsg;
pMsg->next =NULL;
}
else
{
pSafeQue->pTail->next = pMsg;
pMsg->next =NULL;
}
// there is one or more messages in the queue
long previousCount =0;
ReleaseSemaphore(pSafeQue->hMsgSem,
1, //Release Count is 1.
&previousCount //Previous Count. no use for this program
);
UnlockQue(pSafeQue);// safe operation; To give up the right to use the resurece to let another to use it.
return true;
}
//Purpose: To Wait the data in the queue. this function will return until program exits
// or there is one or more data in the queue
//Arguments description:
//pSafeQue --> the pointer to the safe message queue that contains two important semaphores.
//return value: true for success and false for failure
bool WaitDataInQue(pSafeMsgQue pSafeQue)
{
HANDLE hArr[2] ={
pSafeQue->hProExitEvn, //the flag of the program exit
pSafeQue->hMsgSem //semaphore for operation.
};
DWORD dResult = WaitForMultipleObjects( 2, //we have two semaphores
hArr, // the semaphore array
false, //we do not need to wait all the semaphore
INFINITE //wait the semaphore forever until the flag of the semaphore is true;
);
if (dResult - WAIT_OBJECT_0 == 0)
{
return false; //program exits;
}
else if (dResult - WAIT_OBJECT_0 == 1 )
{
return true;
}
else
{
return false;
}
return true;
}
//Purpose: To read and remove a message from the safe message queue
//Arguments description:
//msg --> the message to be gotten from the safe message queue
//pSafeQue --> the pointer to the safe message queue
//return value: true for success and false for failure
bool ReadOneMsg(pMsgQue* pMsg, pSafeMsgQue pSafeQue)
{
if(!WaitDataInQue(pSafeQue))//safe opreation; To get the right to use the resurece
{
return false;
}
if(pSafeQue == NULL || pSafeQue->pHead == NULL) //safe check
{
return false;
}
if(!LockQue(pSafeQue)) // safe opreation; To get the right to use the resurece
{
return false;
}
*pMsg = pSafeQue->pHead;
pMsgQue pTempMsg = pSafeQue->pHead;
pSafeQue->pHead = pTempMsg->next;
pTempMsg->next = NULL;
// memcpy(pMsg, pSafeQue->pHead, sizeof());
if(pSafeQue->pHead != NULL)
{
long previousCount =0;
ReleaseSemaphore(pSafeQue->hMsgSem,
1, //Release Count is 1.
&previousCount //Previous Count. no use for this program
);
}
else
{
pSafeQue->pTail = NULL;
}
UnlockQue(pSafeQue);// safe operation; To give up the right to use the resurece to let another to use it.
return true;
}
//Purpose: To get the right to use the resource of the safe message queue
//Arguments description:
//pSafeQue --> the pointer to the safe message queue that contains two important semaphores.
//return value: true for success and false for failure
bool LockQue(pSafeMsgQue pSafeQue)
{
HANDLE hArr[2] ={
pSafeQue->hProExitEvn, //the flag of the program exit
pSafeQue->hOpSem //semaphore for operation.
};
DWORD dResult = WaitForMultipleObjects( 2, //we have two semaphores
hArr, // the semaphore array
false, //we do not need to wait all the semaphore
INFINITE //wait the semaphore forever until the flag of the semaphore is true;
);
if (dResult - WAIT_OBJECT_0 == 0)
{
return false; //program exits;
}
else if (dResult - WAIT_OBJECT_0 == 1 )
{
return true;
}
else
{
return false;
}
return true;
}
//Purpose: To give up the right to use the resource of the safe message queue
//Arguments description:
//pSafeQue --> the pointer to the safe message queue that contains two important semaphores.
//return value: true for success and false for failure
bool UnlockQue(pSafeMsgQue pSafeQue)
{
// Increment the count of the semaphore.
if (!ReleaseSemaphore(
pSafeQue->hOpSem, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -