hello,
i have written a simple console application just to try boost::thread, i am a multithreading newbie by the way. here is the code
#include <iostream>
#include <boost/thread/thread.hpp>
#include <windows.h>
using namespace std;
void Avg(double * Src, double *Dst, int Per, int Len, string& s )
{
LARGE_INTEGER s1,s2,f;
QueryPerformanceFrequency(&f);
QueryPerformanceCounter(&s1);
for(int i = Per-1; i < Len ; i++)
{
double a = 0;
for(int j = i; j > i-Per ; j--)
a += Src[j];
Dst[i] = a / Per;
}
QueryPerformanceCounter(&s2);
cout << double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 << " ms : "+s << 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 < 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(&f);
QueryPerformanceCounter(&s1);
Avg(a,c,x,L,string("N")); // line 1
bAvg.join(); // line 2
QueryPerformanceCounter(&s2);
cout << double(s2.QuadPart-s1.QuadPart)/f.QuadPart*1000 << " ms : Total" << endl;
delete []a;
delete []b;
delete []c;
system("PAUSE");
return 0;
}
the output of that code is
6621.1 ms : N
6635.28 ms : T
6638.29 ms : Total
Press any key to continue . . .
But when i change the order of line 1 and line 2 the output becomes:
6274.57 ms : T
6250.56 ms : N
12531.3 ms : Total
Press any key to continue . . .
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.
edit:
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?