title: Why boost::recursive_mutex is not working as expected?
body: <p>I have a custom class that uses boost mutexes and locks like this (only relevant parts):</p>
  
  <pre><code>template&lt;class T&gt; class FFTBuf
  {
      public:
          FFTBuf(); 
          [...]
          void lock();
          void unlock();
      private:
          T *_dst;
          int _siglen;
          int _processed_sums;
          int _expected_sums;
          int _assigned_sources;
          bool _written;
          boost::recursive_mutex _mut;
          boost::unique_lock&lt;boost::recursive_mutex&gt; _lock;
  };
  
  template&lt;class T&gt; FFTBuf&lt;T&gt;::FFTBuf() : _dst(NULL), _siglen(0),
      _expected_sums(1), _processed_sums(0), _assigned_sources(0),
      _written(false), _lock(_mut, boost::defer_lock_t())
  {
  }
  
  template&lt;class T&gt; void FFTBuf&lt;T&gt;::lock()
  {
      std::cerr &lt;&lt; "Locking" &lt;&lt; std::endl;
      _lock.lock();
      std::cerr &lt;&lt; "Locked" &lt;&lt; std::endl;
  }
  
  template&lt;class T&gt; void FFTBuf&lt;T&gt;::unlock()
  {
      std::cerr &lt;&lt; "Unlocking" &lt;&lt; std::endl;
      _lock.unlock();
  }
  </code></pre>
  
  <p>If I try to lock more than once the object <strong>from the same thread</strong>, I get an exception (lock_error):</p>
  
  <pre><code>#include "fft_buf.hpp"
  
  int main( void ) {
      FFTBuf&lt;int&gt; b( 256 );
      b.lock();
      b.lock();
      b.unlock();
      b.unlock();
  
      return 0;
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>sb@dex $ ./src/test
  Locking
  Locked
  Locking
  terminate called after throwing an instance of 'boost::lock_error'
     what(): boost::lock_error
  zsh: abort    ./src/test
  </code></pre>
  
  <p>Why is this happening? Am I understanding some concept incorrectly? </p>