title: How to avoid blocking ReaderWriterLockSlim readers when writer is attempting to enter write lock
body: <p>I'm using <code>ReaderWriterLockSlim</code> to guard some operations. I would like to favor readers over writers, so that when a reader holds the lock for long and a writer is attempting to acquire the write lock, further readers down the road are not blocked by the writer's attempt (which is what would happen instead if the writer was blocked on <code>lock.EnterWriteLock()</code>).</p>
  
  <p>To this end, I though that the writer could use <code>TryEnterWriteLock</code> with a short timeout in a loop, so that subsequent readers would still be able to acquire the read lock while the writer can't. However, to my surprise, I found out that an unsuccessful call to <code>TryEnterWriteLock</code> changes the state of the lock, blocking future readers anyway. Proof of concept code:</p>
  
  <pre><code>System.Threading.ReaderWriterLockSlim myLock = new System.Threading.ReaderWriterLockSlim(System.Threading.LockRecursionPolicy.NoRecursion);
  
  System.Threading.Thread t1 = new System.Threading.Thread(() =&gt;
  {
      Console.WriteLine("T1:{0}: entering read lock...", DateTime.Now);
      myLock.EnterReadLock();
      Console.WriteLine("T1:{0}: ...entered read lock.", DateTime.Now);
  
      System.Threading.Thread.Sleep(10000);
  });
  
  System.Threading.Thread t2 = new System.Threading.Thread(() =&gt;
  {
      System.Threading.Thread.Sleep(1000);
  
      while (true)
      {
          Console.WriteLine("T2:{0}: try-entering write lock...", DateTime.Now);
          bool result = myLock.TryEnterWriteLock(TimeSpan.FromMilliseconds(1500));
          Console.WriteLine("T2:{0}: ...try-entered write lock, result={1}.", DateTime.Now, result);
  
          if (result)
          {
              // Got it!
              break;
          }
  
          System.Threading.Thread.Yield();
      }
  
      System.Threading.Thread.Sleep(9000);
  });
  
  System.Threading.Thread t3 = new System.Threading.Thread(() =&gt;
  {
      System.Threading.Thread.Sleep(2000);
  
      Console.WriteLine("T3:{0}: entering read lock...", DateTime.Now);
      myLock.EnterReadLock();
      Console.WriteLine("T3:{0}: ...entered read lock!!!!!!!!!!!!!!!!!!!", DateTime.Now);
  
      System.Threading.Thread.Sleep(8000);
  });
  </code></pre>
  
  <p>The output of this code is:</p>
  
  <pre><code>T1:18-09-2015 16:29:49: entering read lock...
  T1:18-09-2015 16:29:49: ...entered read lock.
  T2:18-09-2015 16:29:50: try-entering write lock...
  T3:18-09-2015 16:29:51: entering read lock...
  T2:18-09-2015 16:29:51: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:51: try-entering write lock...
  T2:18-09-2015 16:29:53: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:53: try-entering write lock...
  T2:18-09-2015 16:29:54: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:54: try-entering write lock...
  T2:18-09-2015 16:29:56: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:56: try-entering write lock...
  T2:18-09-2015 16:29:57: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:57: try-entering write lock...
  T2:18-09-2015 16:29:59: ...try-entered write lock, result=False.
  T2:18-09-2015 16:29:59: try-entering write lock...
  </code></pre>
  
  <p>As you can see, even though thread 2 (the "Writer") hasn't acquired a writer lock and it's not in an <code>EnterWriteLock</code> call, thread 3 gets blocked for good. I can see a similar behavior with <code>ReaderWriterLock</code>.</p>
  
  <p>Am I doing anything wrong? If not, what options do I have to favor readers when a writer is queued?</p>