I was going thorugh implementation of multiple readers /writer lock mentioned here MultiplereadersWriterLock
Here code for EnterReader is
void EnterReader(void)
{
EnterCriticalSection(&m_csWrite);
EnterCriticalSection(&m_csReaderCount);
if (++m_cReaders == 1)
ResetEvent(m_hevReadersCleared);
LeaveCriticalSection(&m_csReaderCount);
LeaveCriticalSection(&m_csWrite);
}
As per my understanding with this lock we are solving problem where we can read shared resource via multiple threads .So it means multiple threads can call EnterReader function and without blocking they can continue to read.
Example
Thread T1 calls EnterReader()
It acquires m_csWrite
It acquires m_csReaderCount
Interrupted by CPU and thread T2 starts
Thread T2 calls EnterReader()
Since m_csWrite already acquired by T1 so how thread T2 can perform read.
T2 cannot proceed further since m_csWrite is already acquired that means T2 reader thread is blocked and waiting for T1 to get finish.
I am confused how this implementation is solving problem of multiple readers accessing shared resource.
EnterReader
just does a quick lock to increment the count of the readers. Notice it releases the locks before returning. When reading, m_csWrite
doesn't stay locked between EnterReader
and LeaveReader
calls like it does between EnterWriter
and LeaveWriter
.
Usage example:
lock->EnterReader();
/*Do the reading*/ //No locks here. m_csReaderCount is blocking writes.
lock->LeaveReader();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.