title: std::timed_mutex::try_lock_for fails immediately
body: <p>I'm using a <code>std::timed_mutex</code> for the first time and it's not behaving the way I expect. It appears to fail immediately instead of waiting for the mutex. I'm providing the lock timeout in milliseconds (as shown here <a href="http://www.cplusplus.com/reference/mutex/timed_mutex/try_lock_for/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/mutex/timed_mutex/try_lock_for/</a>). But the call to <code>try_lock_for()</code> fails right away.</p>
  
  <p>Here's the class that handles locking and unlocking the mutex:</p>
  
  <pre><code>  const unsigned int DEFAULT_MUTEX_WAIT_TIME_MS = 5 * 60 * 1000;
  
    class ScopedTimedMutexLock
    {
    public:
       ScopedTimedMutexLock(std::timed_mutex* sourceMutex, unsigned int numWaitMilliseconds=DEFAULT_MUTEX_WAIT_TIME_MS)
          m_mutex(sourceMutex)
       {
          if( !m_mutex-&gt;try_lock_for( std::chrono::milliseconds(numWaitMilliseconds) ) )
          {
             std::string message = "Timeout attempting to acquire mutex lock for ";
             message += Conversion::toString(numWaitMilliseconds);
             message += "ms";
             throw MutexException(message);
          }
       }
       ~ScopedTimedMutexLock()
       {
          m_mutex-&gt;unlock();
       }
    private:
       std::timed_mutex* m_mutex;
    };
  </code></pre>
  
  <p>And this is where it's being used:</p>
  
  <pre><code>  void  CommandService::Process( RequestType&amp; request )
    {
       unsigned long callTime =
          std::chrono::duration_cast&lt; std::chrono::milliseconds &gt;(
             std::chrono::system_clock::now().time_since_epoch()
          ).count();
  
       try
       {
          ScopedTimedMutexLock lock( m_classMutex, request.getLockWaitTimeMs(DEFAULT_MUTEX_WAIT_TIME_MS) );
          // ... command processing code goes here
       }
       catch( MutexException&amp; mutexException )
       {
          unsigned long catchTime =
             std::chrono::duration_cast&lt; std::chrono::milliseconds &gt;(
                std::chrono::system_clock::now().time_since_epoch()
             ).count();
          cout &lt;&lt; "The following error occured while attempting to process command"
               &lt;&lt; "\n   call  time: " &lt;&lt; callTime
               &lt;&lt; "\n   catch time: " &lt;&lt; catchTime;
          cout &lt;&lt; mutexException.description();
       }
    }
  </code></pre>
  
  <p>Here's the console output:</p>
  
  <pre><code>The following error occured while attempting to process command
     call  time: 1131268914
     catch time: 1131268914
  Timeout attempting to acquire mutex lock for 300000ms
  </code></pre>
  
  <p>Any idea where this is going wrong? Is the conversion to <code>std::chrono::milliseconds</code> correct? How do I make <code>try_lock_for()</code> wait for the lock?</p>
  
  <p><strong>ADDITIONAL INFO:</strong> The call to <code>try_lock_for()</code> didn't <strong>always</strong> fail immediately. Many times the call acquired the lock and everything worked as expected. The failures I was seeing were intermittent. See my answer below for details about why this was failing.</p>