title: ReaderWriterLockSlim vs Double Lock Check pattern
body: <p><strong>EDIT:</strong> From the answers I got already, I understand that the first solution that i presented, not really "Not blocking reads", since only one thread can enter upgradable lock and write lock can not be taken before read is released...</p>
  
  <p>So my question, how to make in correct way the first solution to be "Non blocking read" with creation if not exists?</p>
  
  <hr>
  
  <p>I'm trying to understand two solutions for non blocking multi threading reads. What is the difference between two solutions below (maybe I still not understand some things, but I'm trying):</p>
  
  <pre><code>/// &lt;summary&gt;
  /// ReaderWriterLockSlim pattern
  /// &lt;/summary&gt;
  public class ReadWriteLockCheck
  {
      Dictionary&lt;string, object&gt; _dict = new Dictionary&lt;string, object&gt;();
  
      private ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
  
      public void CreateByKey(string key)
      {
          _rwLock.EnterReadLock();
          try
          {
              if (!_dict.ContainsKey(key)) //Non blocking read - Check if exists
              {
                  _rwLock.EnterWriteLock(); //Lock
                  try
                  {
                      _dict.Add(key, new object());
                  }
                  finally
                  {
                      _rwLock.ExitWriteLock();
                  }
              }
          }
          finally
          {
              _rwLock.ExitReadLock();
          }
      }
  
      public bool GetByKey(string key)
      {
          _rwLock.EnterWriteLock();
          try
          {
              if (_dict.ContainsKey(key)) //Non blocking read
              {
                  return true;
              }
  
              return false;
          }
          finally
          {
              _rwLock.ExitReadLock();
          }
      }
  }
  
  /// &lt;summary&gt;
  /// Double check lock pattern
  /// &lt;/summary&gt;
  public class MonitorLock
  {
      Dictionary&lt;string, object&gt; _dict = new Dictionary&lt;string, object&gt;();
  
      private object _syncObj = new Object();
  
      public void CreateByKey(string key)
      {
          if (!_dict.ContainsKey(key)) //Non blocking read - Check if exists
          {
              Monitor.Enter(_syncObj); //Lock
              try
              {
                  if (!_dict.ContainsKey(key)) //Check if between first check and lock someone already added
                  {
                      _dict.Add(key, new object());
                  }
              }
              finally
              {
                  Monitor.Exit(_syncObj);
              }
          }
      }
  
      public bool GetByKey(string key)
      {
          if (_dict.ContainsKey(key)) //Non blocking read
          {
              return true;
          }
  
          return false;
      }
  }
  </code></pre>
  
  <p>As it looks for me, both of these solutions can make non blocking reads and only blocking when writing...if so, what is a benefit of <code>ReaderWriterLockSlim</code>? As I found in google, <code>Monitor</code> is much faster than <code>ReaderWriterLockSlim</code>.
  Of course I understand that possible I will get incorrect state of dictionary while reading, but it's OK for me.<br>
  Thanks</p>