Although there is a lock MsgList, again afraid after the execution, and multiple threads at the same time to Msg will not be chaos data
I for multithreaded is not very pure, please bosses give directions
CodePudding user response:
BOOL CMsgList: : bPutMsgQuene (char cFrom, char * pData, dwords dwMsgSize, int iIndex, char cKey)
{
The EnterCriticalSection (& amp; M_cs);//lock
If (m_pMsgQueue [m_iQueueTail]!=NULL)
{
LeaveCriticalSection (& amp; M_cs);//unlock
Return FALSE;
}
M_pMsgQueue [m_iQueueTail]=new class CMsg;
If (m_pMsgQueue [m_iQueueTail]==NULL)
{
LeaveCriticalSection (& amp; M_cs);//unlock
Return FALSE;
}
If (m_pMsgQueue [m_iQueueTail] - & gt; BPut (cFrom, pData, dwMsgSize iIndex, cKey)==FALSE)
{
LeaveCriticalSection (& amp; M_cs);//unlock
Return FALSE;
}
M_iQueueTail + +;
If (m_iQueueTail & gt;=DEF_MSGQUEUESIZE m_iQueueTail=0);
LeaveCriticalSection (& amp; M_cs);//unlock
Return TRUE;
}
BOOL CMsg: : bPut (char cFrom, char * pData, dwords dwSize, int iIndex, char cKey)
{
The EnterCriticalSection (& amp; M_cs);//lock
M_pData=https://bbs.csdn.net/topics/new char [dwSize + 1];
If (m_pData=NULL https://bbs.csdn.net/topics/=return FALSE;
ZeroMemory (m_pData dwSize + 1);
Memcpy (m_pData, pData, dwSize);
M_dwSize=dwSize;
M_cFrom=cFrom;
M_iIndex=iIndex;
M_cKey=cKey;
LeaveCriticalSection (& amp; M_cs);//unlock
Return TRUE;
}
Void CMsg: : Get (char * pFrom, char * pData, dwords * pSize, int * pIndex, char * pKey)
{
The EnterCriticalSection (& amp; M_cs);//lock
* pFrom=m_cFrom;
Memcpy (pData, m_pData m_dwSize);
* pSize=m_dwSize;
* pIndex=m_iIndex;
* the pKey=m_cKey;
LeaveCriticalSection (& amp; M_cs);//unlock
}
CodePudding user response:
This is a thread functionvoid CMainLog: : MsgProcess ()
{
The EnterCriticalSection (& amp; M_cs);//lock
While (G_MsgList. BGetMsgQuene (& amp; CFrom, pData, & amp; DwMsgSize, & amp; IClientH, & amp; CKey)==TRUE)
{
There are extracted data the IOCp into the queue processing
}
LeaveCriticalSection (& amp; M_cs);//unlock
CodePudding user response:
Must, if the traffic is bigger, can distinguish between the two locks, read and write locksRead lock is being used, can read, can not write
Write lock is being used, can not read, can't write
CodePudding user response:
"Windows core programming"CodePudding user response: