Wednesday, November 23, 2011

ReaderWriterLock, C# Implementation With a Single Monitor

It turns out, it is not that hard to write a ReaderWriterLock in C# by using just a single monitor object:

public class ReaderWriterLock
{
public ReaderWriterLock()
{
m_activeReaderCount = 0;
m_activeWriter = false;

m_countLock = new object();
}

public void BeginRead()
{
Monitor.Enter(m_countLock);

while (m_activeWriter)
{
Monitor.Wait(m_countLock);
}

m_activeReaderCount++;

Monitor.Exit(m_countLock);
}

public void EndRead()
{
Monitor.Enter(m_countLock);

m_activeReaderCount--;
if (m_activeReaderCount == 0)
{
// At this point we are sure that only writers can be in the
// wait queue, so it is sufficient to wake up just one of them.
Monitor.Pulse(m_countLock);
}

Monitor.Exit(m_countLock);
}

public void BeginWrite()
{
Monitor.Enter(m_countLock);

while ((m_activeReaderCount != 0) || (m_activeWriter))
{
Monitor.Wait(m_countLock);
}

m_activeWriter = true;

Monitor.Exit(m_countLock);
}

public void EndWrite()
{
Monitor.Enter(m_countLock);

m_activeWriter = false;

// Both readers and writers can be in the wait queue.
// We will wake them up all and let them compete for
// the right to read / write.
Monitor.PulseAll(m_countLock);

Monitor.Exit(m_countLock);
}

private int m_activeReaderCount;
private bool m_activeWriter;
private object m_countLock;
}

1 comment:

Anonymous said...

simple and clear implementation... Thanks a lot..