Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The lock should not be part of the protected ressource but of the caller as you have one caller by thread. They <strong>must</strong> use different unique_lock.</p> <p>The purpose of unique_lock is to lock and release the mutex with RAII, so you don't have to call unlock explicitly. </p> <p>When the unique_lock is declared inside a method body, it will belong to the calling thread stack.</p> <p>So a more correct use is :</p> <pre><code>#include &lt;boost/thread/recursive_mutex.hpp&gt; #include &lt;iostream&gt; template&lt;class T&gt; class FFTBuf { public : FFTBuf() { } // this can be called by any thread void exemple() const { boost::recursive_mutex::scoped_lock lock( mut ); std::cerr &lt;&lt; "Locked" &lt;&lt; std::endl; // we are safe here std::cout &lt;&lt; "exemple" &lt;&lt; std::endl ; std::cerr &lt;&lt; "Unlocking ( by RAII)" &lt;&lt; std::endl; } // this is mutable to allow lock of const FFTBuf mutable boost::recursive_mutex mut; }; int main( void ) { FFTBuf&lt; int &gt; b ; { boost::recursive_mutex::scoped_lock lock1( b.mut ); std::cerr &lt;&lt; "Locking 1" &lt;&lt; std::endl; // here the mutex is locked 1 times { boost::recursive_mutex::scoped_lock lock2( b.mut ); std::cerr &lt;&lt; "Locking 2" &lt;&lt; std::endl; // here the mutex is locked 2 times std::cerr &lt;&lt; "Auto UnLocking 2 ( by RAII) " &lt;&lt; std::endl; } b.exemple(); // here the mutex is locked 1 times std::cerr &lt;&lt; "Auto UnLocking 1 ( by RAII) " &lt;&lt; std::endl; } return 0; } </code></pre> <p>Note the mutable on the mutex for const methods.</p> <p>And the boost mutex types have a scoped_lock typedef which is the good unique_lock type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload