Home > Back-end >  Consult about a multi-threaded design problems, every class need to lock? Bosses, please help a look
Consult about a multi-threaded design problems, every class need to lock? Bosses, please help a look

Time:01-05

I want to cancel the lock on the Msg inside, but again afraid

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 function
 void 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 locks
Read 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:

quoted zhao 4, 4/f, the teacher's reply:
"Windows core programming"

You answer like a fart, I could comment on, you can send a book, have what effect?
  • Related