How to Use CriticalSection in MFC

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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories