ReaderWriterLock - how does it work?

liptonIcedTea

Well-known member
Joined
Jan 18, 2007
Messages
89
Programming Experience
1-3
I'm currently doing my MCTS 536 and the book does not really describe ReaderWriterLock too well.

Can someone give me some good examples of what this is used for?

With synlock Me, you specify a reference object to lock (in this case, the instance calling the command)

but with ReaderWriterLock, all you do is specify an interval that it will lock for.

How does it know which resource to actually lock???? does it lock all resources for this amount of time???
 
With a SyncLock block, execution cannot enter if another thread is currently within a SyncLock block that locks on the same object. With a ReaderWriterLock you don't have to specify an item to lock on because the ReaderWriterLock IS an object.

When your code reaches a call to AcquireWriterLock the ReaderWriterLock will check whether:

1. another thread has called AcquireWriterLock and not called ReleaseWriterLock; or
2. one or more other threads have called AcquireReaderLock and not called ReleaseReaderLock.

If either of those conditions is True then AcquireWriterLock will block, i.e. not return, until all the required ReleaseWriterLock or ReleaseReaderLock methods have been called. In short, a thread cannot acquire a lock to write if any other thread is currently writing or reading.

When your code reaches a call to AcquireReaderLock the ReaderReaderLock will check whether another thread has called AcquireWriterLock and not called ReleaseWriterLock. If that condition is True then AcquireReaderLock will block, i.e. not return, until the required ReleaseWriterLock method has been called. In short, a thread cannot acquire a lock to read if any other thread is currently writing, but it can if one or more other threads are reading.

Basically, the ReaderWriterLock class doesn't care what you put between calls to its Acquire and Release methods. It's up to you to use those methods sensibly and only put reading code between calls to AcquireReaderLock and ReleaseReaderLock and to put all your writing code between calls to AcquireWriterLock and ReleaseWriterLock.

The bottom line is that the ReaderWriterLock will only issue one writer lock at a time and then only if there are not currently any reader locks in effect, while it will issue as many reader locks as you like as long as there is currently no writer lock in effect. As such, multiple simultaneous reader threads are allowed but only a single writer thread is allowed with no simultaneous reader threads.
 
You should be aware that when you call an Acquire method on a ReaderWriterLock you are NOT specifying how long it should lock for. You are specifying how long it should wait to acquire the lock before giving up and throwing an exception.

While it's not relevant for your .NET 2.0 exams, you should also note that from .NET 3.5 it is recommended to use the ReaderWriterLockSlim class in place of the ReaderWriterLock class.
 
Back
Top