title: What could cause a deadlock of a single write/multiple read lock?
body: <p>I have a class instance that is used by several other classes in other threads to communicate.</p>
  
  <p>This class uses a slim reader/writer lock (WinAPI's <code>SRWLOCK</code>) as a synchronization object and a couple of RAII helper classes to actually lock/unlock the thing:</p>
  
  <pre><code>static unsigned int readCounter = 0;
  
  class CReadLock
  {
  public:
      CReadLock(SRWLOCK&amp; Lock) : m_Lock(Lock) { InterlockedIncrement(&amp;readCounter); AcquireSRWLockShared(&amp;m_Lock);  }
      ~CReadLock() {ReleaseSRWLockShared(m_Lock); InterlockedDecrement(&amp;readCounter);}
  
  private:
      SRWLOCK&amp; m_Lock;
  };
  
  class CWriteLock
  {
  public:
      CWriteLock(SRWLOCK&amp; Lock) : m_Lock(Lock) { AcquireSRWLockExclusive(&amp;m_Lock); }
      ~CWriteLock() { ReleaseSRWLockExclusive(&amp;m_Lock); }
  
  private:
      SRWLOCK&amp; m_Lock;
  };
  </code></pre>
  
  <p>The problem is the whole thing deadlocks all the time. When I pause the deadlocked program, I see:</p>
  
  <ul>
  <li>one thread stuck in <code>AcquireSRWLockExclusive()</code>;</li>
  <li>two threads stuck in <code>AcquireSRWLockShared()</code>;</li>
  <li><code>readCounter</code> global is set to 3.</li>
  </ul>
  
  <p>The way I see it, the only way for this to happen is <code>CReadLock</code> instance's destructor hasn't been called somehow somewhere so the lock is perpetually stuck. However, the only way for this to happen (as far as I know) is because an exception has been thrown. It wasn't. I checked.</p>
  
  <p>What might be the problem? How should I go about fixing (or at least locating the reason of) this thing?</p>