Project Description
ReaderWriterLocker which cannot be hard locked. Meant to replace ReaderWriterLock and ReaderWriterLockSlim so you don't have to be bothered by upgrade locks.

Forked from jpmikkers' ReaderWriterLockAlt located at http://readerwriterlockalt.codeplex.com/.  If you would like to learn how to design your own, or you would like to help with this project, please start there.

It supports .NET 2.0+.  And in the future, this code will be provided in VB.net, C# .net, and pending library selection, gnu gcc C++.

License
This project uses a new Tri-License Choice MIT/L-GPL/MS-PL.  As such, you have a choice of choosing MIT, L-GPL v2 or v3, or MS-PL. Or you can choose to use the tri-license and let others have their choice. While using the tri-license, MIT preceeds L-GPL and MS-PL. Any conflicts between the three will be resolved by using MIT first, L-GPL second, and MS-PL third (order of least restrictive licenses).

Example Syntax
VB.NET
' Untested code...

Imports System
Imports System.Threading
Imports System.Collections.Generic

Dim Lock    As New ThreadSafeReaderWriterLock(True)
Dim List      As New List(Of Integer)(New Integer {0,5,0,4,0,3,0,2,0,1})
Dim IsBusy As Integer = 0

Public Sub InitalizeThreads()
  For Index As Integer = 0 To 25
    Interlocked.Increment(IsBusy)
    ThreadPool.QueueUserWorkItem(FunctionToRunInMultipleThreads, Index)
  Next

  While IsBusy > 0
    Thread.Sleep(500)
  End While
End Sub

Public Sub FunctionToRunInMultipleThreads(threadContext As Object)
  Dim threadIndex As Integer = CInt(threadContext)
  Console.WriteLine("thread {0} started...", threadIndex);

  Using ReadLock As IDisposable = Lock.GetReadLock()
    ' Iterate through list
    For Index As Integer = 0 To List.Count - 1
      List(Index) += Index
    Next

    ' Upgrade read lock to write lock.  Yes.  It is that simple.
    Using WriteLock As IDisposable = Lock.GetWriteLock()
      For Index As Integer = 0 To List.Count - 1
        List.Add(List(Index))
      Next
    End Using

    ' Iterate through list
    For Index As Integer = 0 To List.Count - 1
      Console.WriteLine("List value: {0}, on thread {1}.", List(Index), threadIndex)
    Next
  End Using

  Interlocked.Decrement(IsBusy)
End Sub

Last edited Sep 15, 2010 at 12:33 AM by TamusJRoyce, version 7