Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use Boost.Thread's <a href="http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref" rel="nofollow">condition variables</a> to synchronize on a condition more complex than what <code>join</code> can provide. Here's a example based on yours:</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/thread.hpp&gt; #include &lt;boost/thread/locks.hpp&gt; #include &lt;boost/thread/mutex.hpp&gt; #include &lt;boost/thread/condition_variable.hpp&gt; boost::mutex mutex; boost::condition_variable cond; // These three variables protected by mutex bool finishedFlag = false; int finishedID = 0; int finishedCount = 0; int id=1; void temp() { int threadID = id++; for(int i=0;i&lt;3;i++) { std::cout &lt;&lt; threadID &lt;&lt; " : " &lt;&lt; i &lt;&lt; std::endl; boost::this_thread::sleep(boost::posix_time::millisec(100)); } { boost::lock_guard&lt;boost::mutex&gt; lock(mutex); finishedFlag = true; finishedID = threadID; ++finishedCount; } cond.notify_one(); } int main(void) { boost::thread thrd1(temp); boost::this_thread::sleep(boost::posix_time::millisec(300)); boost::thread thrd2(temp); boost::thread thrd3(temp); boost::unique_lock&lt;boost::mutex&gt; lock(mutex); while (finishedCount &lt; 3) { while (finishedFlag != true) { // mutex is released while we wait for cond to be signalled. cond.wait(lock); // mutex is reacquired as soon as we finish waiting. } finishedFlag = false; if (finishedID == 1) { // Do something special about thrd1 finishing std::cout &lt;&lt; "thrd1 finished" &lt;&lt; std::endl; } }; // All 3 threads finished at this point. return 0; } </code></pre>
    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. 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