Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::threads execution ordering
    text
    copied!<p>i have a problem with the order of execution of the threads created consecutively. here is the code.</p> <pre><code>#include &lt;iostream&gt; #include &lt;Windows.h&gt; #include &lt;boost/thread.hpp&gt; using namespace std; boost::mutex mutexA; boost::mutex mutexB; boost::mutex mutexC; boost::mutex mutexD; void SomeWork(char letter, int index) { boost::mutex::scoped_lock lock; switch(letter) { case 'A' : lock = boost::mutex::scoped_lock(mutexA); break; case 'B' : lock = boost::mutex::scoped_lock(mutexB); break; case 'C' : lock = boost::mutex::scoped_lock(mutexC); break; case 'D' : lock = boost::mutex::scoped_lock(mutexD); break; } cout &lt;&lt; letter &lt;&lt;index &lt;&lt; " started" &lt;&lt; endl; Sleep(800); cout &lt;&lt; letter&lt;&lt;index &lt;&lt; " finished" &lt;&lt; endl; } int main(int argc , char * argv[]) { for(int i = 0; i &lt; 16; i++) { char x = rand() % 4 + 65; boost::thread tha = boost::thread(SomeWork,x,i); Sleep(10); } Sleep(6000); system("PAUSE"); return 0; } </code></pre> <p>each time a letter (from A to D) and a genereaion id (i) is passed to the method SomeWork as a thread. i do not care about the execution order between letters but for a prticular letter ,say A, Ax has to start before Ay, if x &lt; y. a random part of a random output of the code is :</p> <pre> B0 started D1 started C2 started A3 started B0 finished B12 started D1 finished D15 started C2 finished C6 started A3 finished A9 started B12 finished B11 started --> B11 started after B12 finished. D15 finished D13 started C6 finished C7 started A9 finished </pre> <p>how can avoid such conditions?<br> thanks.</p> <hr> <p>i solved the problem using condition variables. but i changed the problem a bit. the solution is to keep track of the index of the for loop. so each thread knows when it does not work. but as far as this code is concerned, there are two other things that i would like to ask about.<br> first, on my computer, when i set the for-loop index to 350 i had an access violation. 310 was the number of loops, which was ok. so i realized that there is a maximum number of threads to be generated. how can i determine this number? second, in visual studio 2008, the release version of the code showed a really strange behaviour. without using condition variables (lines 1 to 3 were commented out), the threads were ordered. how could that happen?</p> <p>here is the code: </p> <pre><code>#include &lt;iostream&gt; #include &lt;Windows.h&gt; #include &lt;boost/thread.hpp&gt; using namespace std; boost::mutex mutexA; boost::mutex mutexB; boost::mutex mutexC; boost::mutex mutexD; class cl { public: boost::condition_variable con; boost::mutex mutex_cl; char Letter; int num; cl(char letter) : Letter(letter) , num(0) { } void doWork( int index, int tracknum) { boost::unique_lock&lt;boost::mutex&gt; lock(mutex_cl); while(num != tracknum) // line 1 con.wait(lock); // line 2 Sleep(10); num = index; cout &lt;&lt; Letter&lt;&lt;index &lt;&lt; endl; con.notify_all(); // line 3 } }; int main(int argc , char * argv[]) { cl A('A'); cl B('B'); cl C('C'); cl D('D'); for(int i = 0; i &lt; 100; i++) { boost::thread(&amp;cl::doWork,&amp;A,i+1,i); boost::thread(&amp;cl::doWork,&amp;B,i+1,i); boost::thread(&amp;cl::doWork,&amp;C,i+1,i); boost::thread(&amp;cl::doWork,&amp;D,i+1,i); } cout &lt;&lt; "************************************************************************" &lt;&lt; endl; Sleep(6000); system("PAUSE"); return 0; } </code></pre>
 

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