By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Impossible to hard lock Read-Write Locking

100+
P: 110
http://nohardlockrwlocker.codeplex.com/ is a read-write locker I recently designed so hard locks are impossible.

It is meant to replace ReaderWriterLock and ReaderWriterLockSlim. Upgrade locks are as simple as placing a write lock inside a read lock.

It will soon have collections which utilize this locker to cache modifications while enumerations occurs to it. Then after it completes, modifications are applied. This makes enumeration while modification impossible. Even when thread safety turned off inside the locker.

It also supports setting a thread (such as the UI thread) as a priority thread, so when hard locks do occur, that thread is the one chosen to break the hard lock.

In addition, the read-write locker allows being a drop-in replacement for ReaderWriterLock and ReaderWriterLockSlim by renaming them to NHLReaderWriterLock and NHLReaderWriterLockSlim respectfully (where NHL stands for No Hard Locking). As for hard locks happening to regular SyncLocks/locks, it supports replacing those with non-hard locking regular locks.

I hope people find this useful over the poorly implemented ReaderWriterLock/ReaderWriterLockSlim which allows hard locks and instability.

Expand|Select|Wrap|Line Numbers
  1. ReadWriteLocker Lock = New ReadWriteLocker(True);
  2.  
  3. using (IDisposable ReadLock = Lock.GetReadLock())
  4. {
  5.   // Perform any reading you might need, like iterating through a List.
  6.  
  7.   using (IDisposable WriteLock = Lock.GetWriteLock())
  8.   {
  9.     // Perform any writing you may need, like adding items to a List.
  10.  
  11.   } // End using
  12. } // End using
  13.  
Hopefully helpful,

TamusJRoyce
Sep 22 '10 #1
Share this Article
Share on Google+