2
Answers

How to use Critical Section in MFC

Abhi MFC

Abhi MFC

15y
6.5k
1
Hi 

I have two worker threads, where one thread(WriteBuf) is writing into buffer and another thread(ReadBuf) is reading from the same buffer.

I have created CriticalSection object like CCriticalSection m_crit created globaly.

My thread code looks like below

UINT WriteBuf(LPVOID pParam)



crit.Lock();
buf[0] = 'a'; //Writing to buffer.
return TRUE;
}


UINT ReadBuf(LPVOID pParam)



buf1[0] = buf[0]; Reading from the buffer.
TRACE("%c",buf1[0]);
return TRUE;

}

Please let me know is this the rite way to lock the resource(buffer),coz i havent unlocked yet but still i can able to read the buffer from another thread(ReadBuf) .

So please help me to understand the critical section usage here.

Thanks
Abhi
Answers (2)