This is a theoretical question; I don't have an actual issue to solve, I just want to understand the reasoning behind the way this works.
I found out that if a thread omits to exit a ReaderWriterLock then other threads will fail to acquire a lock even after the original thread terminates. Is there a good reason why ReaderWriterLock does not grant the lock to waiting threads as soon as the thread that owns the lock terminates?
Here is a test case that demonstrate the issue.
static void Main(string[] args)
{
ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
Thread t1 = new Thread((a) =>
{
readerWriterLock.EnterReadLock();
// this thread omits to Exit the lock....
});
Thread t2 = new Thread((a) =>
{
readerWriterLock.EnterWriteLock();
});
t1.Start();
t2.Start();
// wait for all threads to finish
t1.Join();
t2.Join();
}