title: How to use a lock_guard with try_lock_for
body: <p>I can use <code>boost::lock_guard</code> to acquire a lock on a <code>boost::mutex</code> object and this mechanism will ascertain that once the <code>boost::lock_guard</code> goes out of scope the lock will be released:</p>
  
  <pre><code>{
    boost::lock_guard&lt;boost::mutex&gt; lock(a_mutex);
    // Do the work
  }
  </code></pre>
  
  <p>In this case, <code>a_mutex</code> will be released regardless of whether the code block was exited by an exception or not.</p>
  
  <p>On the other hand, a <code>boost::timed_mutex</code> also supports a method <code>try_lock_for(period)</code>, e.g.</p>
  
  <pre><code>if(a_timed_mutex.try_lock_for(boost::chrono::seconds(1))) {
    // Do the work
    a_timed_mutex.unlock(); // &lt;- This is needed!
  } else {
    // Handle the acquisition failure
  }
  </code></pre>
  
  <p>This code will not <code>unlock()</code> the <code>a_timed_mutex</code> if the <code>true</code> block of the <code>if</code> statement is exited with an exception.</p>
  
  <p><strong>Question:</strong> Boost (and, as far as I can see, neither the C++11 standard) do not seem to offer a variant of <code>lock_guard</code> that works with <code>try_lock()</code> or <code>try_lock_for(period)</code>. What is the "recommended" way or best practice to handle the second situation?</p>