Note that there are some explanatory texts on larger screens.

plurals
  1. POManually releasing boost locks?
    primarykey
    data
    text
    <p>For the sake of learning combinatorics of boost::thread I'm implementing a simple barrier (BR) for threads which lock a common mutex (M). However, as far as I get it when going to BR.wait() the locks on the mutex are not released, so in order for all threads to arrive at BR a manual release of the lock on M needs to be performed. So I have the following code: </p> <pre><code>boost::barrier BR(3); boost::mutex M; void THfoo(int m){ cout&lt;&lt;"TH"&lt;&lt;m&lt;&lt;" started and attempts locking M\n"; boost::lock_guard&lt;boost::mutex&gt; ownlock(M); cout&lt;&lt;"TH"&lt;&lt;m&lt;&lt;" locked mutex\n"; Wait_(15); //simple wait for few milliseconds M.unlock(); //probably bad idea //boost::lock_guard&lt;boost::mutex&gt; ~ownlock(M); // this TH needs to unlock the mutex before going to barrier BR cout&lt;&lt;"TH"&lt;&lt;m&lt;&lt;" unlocked mutex\n"; cout&lt;&lt;"TH"&lt;&lt;m&lt;&lt;" going to BR\n"; BR.wait(); cout&lt;&lt;"TH"&lt;&lt;m&lt;&lt;" let loose from BR\n"; } int main() { boost::thread TH1(THfoo,1); boost::thread TH2(THfoo,2); boost::thread TH3(THfoo,3); TH2.join(); //but TH2 might end before TH1, and so destroy BR and M cout&lt;&lt;"exiting main TH \n"; return 0; } </code></pre> <p>Whereas M.unlock() is clearly a bad solution (not using the lock); so how to (simply) release the lock? Also: how do I (properly) wait in main() for all threads to finish? (TH2.join() is bad, `cause TH2 may finish first...); </p> <p>Please do not suggest go-arounds, e.g. with conditional variables, which I can also use, but it must be possible to do it straightforwardly without them. </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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