title: Is it possible to determine is a boost::basic_lockable_adapter is currently locked?
body: <p>We've been using Boost's <code>basic_lockable_adapter</code> and <code>strict_lock</code> in our project for thread synchronization. We've determined a situation that could possibly lead to a deadlock, and we want to avoid this situation. If we were able to detect if the <code>basic_lockable_adapter</code> was locked or not, we could assert on that and catch it during development.</p>
  
  <p>Here is an example of what I'm looking for (this code is compilable, except for the fact that one function doesn't exist):</p>
  
  <pre><code>#include &lt;boost/thread/lockable_adapter.hpp&gt;
  #include &lt;boost/thread/strict_lock.hpp&gt;
  #include &lt;mutex&gt;
  
  class Controller : public boost::basic_lockable_adapter&lt;std::recursive_mutex&gt;
  {
  public:
      static Controller&amp; instance() { static Controller instance; return instance; }
  };
  
  void check_for_lock()
  {
      // is_locked() isn't real ... but I'd like that functionality somehow
      if (Controller::instance().is_locked())
          std::cout &lt;&lt; "It's locked." &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; "Not locked." &lt;&lt; std::endl;
  }
  
  int main()
  {
      check_for_lock();
      boost::strict_lock&lt;Controller&gt; lock(Controller::instance());
      check_for_lock();
  
      return 0;
  }
  </code></pre>
  
  <p>Is it possible to implement the equivalent of <code>.is_locked()</code>?</p>
  
  <p><strong>Edit - Clarification</strong></p>
  
  <p>I forgot to mention an important point. We use the "external locking" paradigm as explained in the Boost documentation. With that, a function that absolutely needs to hold the lock simply expects a reference as a parameter - therefore, if you get the parameter, you know you have the lock.</p>
  
  <p>In my case, it's the opposite. I want to know whether <em>the current thread currently has a lock</em>, from a function that does not expect the <code>strict_lock</code> as a parameter, so I can assert or throw.</p>
  
  <p>So: The function knows what the <code>lockable_adapter</code> is, but needs to know if the currently executing thread has that lock or not (it doesn't care about other threads).</p>