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?
    primarykey
    data
    text
    <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>
    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. COGiven the questions you are asking, it may be a good idea for you to take a step back and spend some time really getting your head around threading. It is the portal to a whole world of bugs and pain if you don't quite understand what's going on, and you probably don't want that in trading code. Can you maybe write a single-threaded application for now? Have you proven you need multiple threads? Naive threaded applications often end up slower than their unthreaded counterparts anyway, due to communication costs.
      singulars
    2. CO@chrispy: as a matter of fact my single threaded applcation is ready. but my concern is what if a receive a signal during calculations of a signal just received. the answer i come up with is just openning another thread and taking care of the signal on a another cpu core if available. is my answer wrong?
      singulars
    3. COA simple design that may work for you: have one thread to handle incoming signals and queue them for processing, and another to pull them off the queue and process them sequentially. Perhaps you can get the tcp library to do the queueing for you, though? i.e. if possible, leave the signals until you are ready to process them. I don't know the details of your tcp library. This will insulate you from threading details.
      singulars
 

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