Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat is the execution order of a threaded and non-threaded function call?
    text
    copied!<p>i have written a simple console application just to try boost::thread, i am a multithreading newbie by the way. here is the code</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/thread/thread.hpp&gt; #include &lt;windows.h&gt; using namespace std; void Avg(double * Src, double *Dst, int Per, int Len, string&amp; s ) { LARGE_INTEGER s1,s2,f; QueryPerformanceFrequency(&amp;f); QueryPerformanceCounter(&amp;s1); for(int i = Per-1; i &lt; Len ; i++) { double a = 0; for(int j = i; j &gt; i-Per ; j--) a += Src[j]; Dst[i] = a / Per; } QueryPerformanceCounter(&amp;s2); cout &lt;&lt; double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 &lt;&lt; " ms : "+s &lt;&lt; endl; } int main(int argc, char* argv[]) { int L = 200000; double * a = new double[L], *b = new double[L] , *c = new double[L]; for(int i =0; i &lt; L;i++) { a[i] = i+2; } int x = 10000; boost::thread bAvg(Avg,a,b,x,L,string("T")); LARGE_INTEGER s1,s2,f; QueryPerformanceFrequency(&amp;f); QueryPerformanceCounter(&amp;s1); Avg(a,c,x,L,string("N")); // line 1 bAvg.join(); // line 2 QueryPerformanceCounter(&amp;s2); cout &lt;&lt; double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 &lt;&lt; " ms : Total" &lt;&lt; endl; delete []a; delete []b; delete []c; system("PAUSE"); return 0; } </code></pre> <p>the output of that code is </p> <blockquote> <p>6621.1 ms : N<br> 6635.28 ms : T<br> 6638.29 ms : Total<br> Press any key to continue . . .</p> </blockquote> <p>But when i change the order of line 1 and line 2 the output becomes: </p> <blockquote> <p>6274.57 ms : T<br> 6250.56 ms : N<br> 12531.3 ms : Total<br> Press any key to continue . . .</p> </blockquote> <p>in case 1, bAvg.join() is fired just after Avg fired and before it is finished. i thought the results of case 1 and case 2 would be the opposite of each other. this has something to do with the execution order of threads i think. </p> <p><strong>edit:</strong><br> actually i am planning to write an application for trading purposes. i am going to create, say, at least 10 threads of calculations per upcoming signal for a single stock. the signals will be received via tcp connection. but if the main execution thread waits for the completion of other sub-threads', then a single signal may be easily missed, because the main thread is idle. how am i supposed to handle the signals and run the calculation threads?</p>
 

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