title: EnterCriticalSection Deadlocking
body: <p>I found some code that claimed to be able to make a thread sleep for an accurate amount of time. Testing the code out, it seems to work great, however it always deadlocks after a short amount of time.</p>
  
  <p>Here is the original code. I put prints before entering and leaving the critical section, and saw that sometimes it leaves or enters twice in a row. It seems to deadlock at the EnterCriticalSection call within the Wait function.</p>
  
  <p>Is there a way I can modify this code to retain its functionality while not deadlocking?</p>
  
  <pre class="lang-cpp prettyprint-override"><code>//----------------------------------------------------------------
  class PreciseTimer
  {
  public:
     PreciseTimer() : mRes(0), toLeave(false), stopCounter(-1)
     {
        InitializeCriticalSection(&amp;crit);
        mRes = timeSetEvent(1, 0, &amp;TimerProc, (DWORD)this,
                            TIME_PERIODIC);
     }
     virtual ~PreciseTimer()
     {
        mRes = timeKillEvent(mRes);
        DeleteCriticalSection(&amp;crit);
     }
  
     ///////////////////////////////////////////////////////////////
     // Function name   : Wait
     // Description     : Waits for the required duration of msecs.
     //                 : Timer resolution is precisely 1 msec
     // Return type     : void  :
     // Argument        : int timeout : timeout in msecs
     ///////////////////////////////////////////////////////////////
     void Wait(int timeout)
     {
        if ( timeout )
        {
           stopCounter = timeout;
           toLeave = true;
           // this will do the actual delay - timer callback shares
           // same crit section
           EnterCriticalSection(&amp;crit);
           LeaveCriticalSection(&amp;crit);
        }
     }
     ///////////////////////////////////////////////////////////////
     // Function name   : TimerProc
     // Description     : Timer callback procedure that is called
     //                 : every 1msec
     //                 : by high resolution media timers
     // Return type     : void CALLBACK  :
     // Argument        : UINT uiID :
     // Argument        : UINT uiMsg :
     // Argument        : DWORD dwUser :
     // Argument        : DWORD dw1 :
     // Argument        : DWORD dw2 :
     ///////////////////////////////////////////////////////////////
     static void CALLBACK TimerProc(UINT uiID, UINT uiMsg, DWORD
                                    dwUser, DWORD dw1, DWORD dw2)
     {
        static volatile bool entered = false;
  
        PreciseTimer* pThis = (PreciseTimer*)dwUser;
        if ( pThis )
        {
           if ( !entered &amp;&amp; !pThis-&gt;toLeave )   // block section as
                                                // soon as we can
           {
              entered = true;
              EnterCriticalSection(&amp;pThis-&gt;crit);
           }
           else if ( pThis-&gt;toLeave &amp;&amp; pThis-&gt;stopCounter == 0 )
                                                // leave section
                                                // when counter
                                                // has expired
           {
              pThis-&gt;toLeave = false;
              entered = false;
              LeaveCriticalSection(&amp;pThis-&gt;crit);
           }
           else if ( pThis-&gt;stopCounter &gt; 0 )   // if counter is set
                                                // to anything, then
                                                // continue to drop
                                                // it...
              --pThis-&gt;stopCounter;
        }
     }
  
  private:
     MMRESULT         mRes;
     CRITICAL_SECTION crit;
     volatile bool    toLeave;
     volatile int     stopCounter;
  };
  </code></pre>
  
  <p></p>