title: Nested locks (critical section) not working
body: <p>I am learning about Critical Section (for the purpose of multithreading) and I found a class online using it. I don't understand why my code doesn't work though - I should get "success" on the console display but I don't.</p>
  
  <p>Am I locking it incorrectly? I am sure I am entering and exiting the sections accurately - but I do not know why the third thread (<code>mul</code>) does not seem to work.</p>
  
  <p>Here is the main code (doing this on VS 2012):</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;process.h&gt;
  #include &lt;iostream&gt;
  #include &lt;assert.h&gt;
  #include &lt;queue&gt;
  #include "Lock.h"
  
  //File: CriticalSectionExample.cpp
  
  #define MAX_THREADS 2
  
  using namespace std;
  
  static unsigned int counter = 100;
  static bool alive = true;
  static examples::Lock lock_1;
  static examples::Lock lock_2;
  
  queue&lt;int&gt; test_q;
  queue&lt;int&gt; later_q;
  
  static unsigned __stdcall sub(void *args)
  {
      while(alive)
      {
          cout &lt;&lt; "tq";
          lock_1.acquire();
          test_q.push(1);
          lock_1.release();
  
          ::Sleep(500);
      }
      return 0;
  }
  
  static unsigned __stdcall add(void *args)
  {
      while(alive)
      {
          if (!test_q.empty())
          {
              int first = test_q.front();
              //cout &lt;&lt; first &lt;&lt; endl;
  
              lock_1.acquire();
              test_q.pop();
              lock_1.release();
  
              lock_2.acquire();
              cout &lt;&lt; "lq" &lt;&lt; first &lt;&lt; endl;
              later_q.push(first);
              lock_2.release();
          }
  
          ::Sleep(500);
      }
      return 0;
  }
  
  static unsigned __stdcall mul(void *args)
  {
      while(alive)
      {
          if (!later_q.empty())
          {
              cout &lt;&lt; "success" &lt;&lt; endl;
              lock_2.acquire();
              test_q.pop();
              lock_2.release();
          }
  
          ::Sleep(500);
      }
      return 0;
  }
  
  int main()
  {
      // create threads
      unsigned tadd;
      HANDLE hadd = (HANDLE) ::_beginthreadex(0, 0, &amp;add, 0, CREATE_SUSPENDED, &amp;tadd);
      assert(hadd != 0);
  
      unsigned tsub;
      HANDLE hsub = (HANDLE) ::_beginthreadex(0, 0, &amp;sub, 0, CREATE_SUSPENDED, &amp;tsub);
      assert(hsub != 0);
  
       unsigned tmul;
      HANDLE hmul = (HANDLE) ::_beginthreadex(0, 0, &amp;mul, 0, CREATE_SUSPENDED, &amp;tsub);
      assert(hmul != 0);
  
      // start threads
      ::ResumeThread(hadd);
      ::ResumeThread(hsub);
  
      ::Sleep(10000); // let threads run for 10 seconds
  
      // stop &amp; cleanup threads
      alive = false;
      ::WaitForSingleObject(hsub, INFINITE);
      ::CloseHandle(hsub);
      ::WaitForSingleObject(hadd, INFINITE);
      ::CloseHandle(hadd);
  
      return 0;
  }
  </code></pre>
  
  <p>and this is the header file including Critical Section:</p>
  
  <pre><code>#ifndef _Lock_H_
  #define _Lock_H_
  
  
  #include &lt;windows.h&gt;
  
  /**
  *@description: A simple Lock implementation using windows critical section object
  */
  
  namespace examples
  {
      class Lock
      {
      public:
          Lock()
          {
              ::InitializeCriticalSection(&amp;m_cs);
          }
  
          ~Lock()
          {
              ::DeleteCriticalSection(&amp;m_cs);
          }
  
          void acquire()
          {
              ::EnterCriticalSection(&amp;m_cs);
          }
  
          void release()
          {
              ::LeaveCriticalSection(&amp;m_cs);
          }
  
      private:
          Lock(const Lock&amp;);
          Lock&amp; operator=(const Lock&amp;);
  
          CRITICAL_SECTION m_cs;
      };
  
  }
  
  #endif //_Lock_H_
  </code></pre>