Project Description
This ReaderWriterLock is an improved alternative to the ReaderWriterLock of the .NET 2.0 framework. It corrects the main problem of ReaderWriterLock in that it gives priority to threads that request a Write Lock. Also, it's faster!

Features:
  • write locks have priority over read locks
  • open source, permissive MIT license, so it's usable in commercial projects too.
  • supports lock recursion (for details see: lock recursion)
  • faster than ReaderWriterLock (by about 20% to 30% on my machine)
  • simple implementation using only Monitor.Lock and Monitor.Pulse

When to use:
  • If you cannot use the ReaderWriterLockSlim alternative of the later framework versions because you are stuck with .Net 2.0
  • in situations where there are more readers than writers to a piece of data that has to be made threadsafe.
  • if you're getting lousy performance of the old ReaderWriterLock because it gives priority to reader threads.

How to use:
using CodePlex.JPMikkers;
...
public class Demo
{
    private readonly ReaderWriterLockAlt m_Lock = new ReaderWriterLockAlt(); 
    ...
    public Demo()
    {        
    }
    ...
    public void ReadExample()
    {
        // acquire a read lock:
        using(m_Lock.ReadLock())
        {
            // protected reading code goes here..
        }
    }
    ...
    public void WriteExample()
    {
        // acquire a write lock:
        using(m_Lock.WriteLock())
        {
            // protected writing code goes here..
        }
    }
}

Last edited Dec 18, 2008 at 11:02 PM by jpmikkers, version 6