471,071 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,071 developers and data experts.

Impossible to hard lock Read-Write Locking

110 100+
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
0 2723

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Antoon Pardon | last post: by
2 posts views Thread by /.. | last post: by
3 posts views Thread by Paul Close | last post: by
5 posts views Thread by Uwe C. Schroeder | last post: by
54 posts views Thread by smnoff | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.