A reader-writer lock implementation designed to support a limited number of concurrent readers, and a single writer. If more than one writer attempts to acquire the lock at the same time, the behavior is undefined.
Example #1
0
 static void Main(string[] args)
 {
     ThreadPool.QueueUserWorkItem(_ =>
     {
         MySmartReaderWriterLock rwl1 = new MySmartReaderWriterLock(10);
         Console.WriteLine("Work item 1 locking for writing...");
         rwl1.LockForWriting();
         Console.WriteLine("Work item 1 locked for writing");
         rwl1.UnlockForWriting();
         Console.WriteLine("Work item 1 done");
     });
     ThreadPool.QueueUserWorkItem(_ =>
     {
         MySmartReaderWriterLock rwl2 = new MySmartReaderWriterLock(10);
         Console.WriteLine("Work item 2 locking for writing...");
         rwl2.LockForWriting();
         Console.WriteLine("Work item 2 locked for writing");
         rwl2.UnlockForWriting();
         Console.WriteLine("Work item 2 done");
     });
     Console.ReadLine();
 }
Example #2
0
 static void Main(string[] args)
 {
     ThreadPool.QueueUserWorkItem(_ =>
     {
         MySmartReaderWriterLock rwl1 = new MySmartReaderWriterLock(10);
         Console.WriteLine("Work item 1 locking for writing...");
         rwl1.LockForWriting();
         Console.WriteLine("Work item 1 locked for writing");
         rwl1.UnlockForWriting();
         Console.WriteLine("Work item 1 done");
     });
     ThreadPool.QueueUserWorkItem(_ =>
     {
         MySmartReaderWriterLock rwl2 = new MySmartReaderWriterLock(10);
         Console.WriteLine("Work item 2 locking for writing...");
         rwl2.LockForWriting();
         Console.WriteLine("Work item 2 locked for writing");
         rwl2.UnlockForWriting();
         Console.WriteLine("Work item 2 done");
     });
     Console.ReadLine();
 }