Multthreading

Hi,

I am new to c++ and multithreading in c++. I have implemented an API to get the market live data. I want to make that application multithreaded. SO far my application is working fine.

I want to apply some mathematical functions on the live market data i receive by calling a function repeatedly say after 30milliseconds. For that purpose i start writing a thread to call that function after every thirty second.

What i did was i created that thread in main function as the application is receiving the live data throughput the execution of the main thread. But the thread seems to be not working.

If some one can help me on this.

If you want i can paste my code here.

Thanks.
Wy do you think it's not working?
I check it by disabling my code that connects me to the client to capture the data. In that scenario it works but when i'm getting live data continuously, the thread wont be initiated or you can say doent call the funtion i want to execute while connected to the client.

Thanks.
Without seeing what you've done, I can't make a meaningful comment.
Now the threads are working fine. I just want to execute the thread two and thread 3 after every 30 milliseconds on the data gathered by the thread 1 which will keep running throughout the process.

How im goona do that. This code will execute the thread 2 and thread 3 before the thread 1 which i dont want.

Any suggestion about that. the code of my main is below.


This is my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
int main( int argc, char** argv )
{
  if ( argc != 2 )
  {
    std::cout << "usage: " << argv[ 0 ]
    << " FILE." << std::endl;
    return 0;
  }
  std::string file = argv[ 1 ];

  try
  {
	
    FIX::SessionSettings settings( file );
	
    CKApplication application(30);
	//CKApplication * t1 = new CKApplication(30);
    FIX::FileStoreFactory storeFactory( settings );
	
    FIX::SocketInitiator initiator( application, storeFactory, settings );
	
	application.setSessionSetting(settings);
	std::string symbol;
	std::string symbol1;
	//application.setHeader();
	 initiator.start();
	 Sleep(10000);
	/* char action;
	while(true){		
	  std::cout << std::endl
			  << "1) Market data Request" << std::endl
			  << "4) Quit" << std::endl
			  << "Action: ";
			  std::cin >> action;
			  if(action == '1')
				  while(application.LoginStatus && application.subscribestatus)
				 {
		
					// application.Trading_Session();
					 //Sleep(30000);
						application.subscribe();
				 }		
			  else if (action == '2')
				  exit(0);
			  else
				  break;
	}*/

	 std::ifstream fin;
	 fin.open("CurrencyPairs.txt");
	 if(!fin)
	 {
		 std::cout<<"File not found"<<endl;
		 return 0;
	 }
	 unsigned uiThreadID;
	HANDLE hThread;
	  while(!fin.eof())
	 {
			fin>> symbol;
			application.Create_CSV(symbol);		
			//application.subscribe (symbol);					
			hThread = (HANDLE)_beginthreadex(NULL,0,CKApplication::subscribe,&application,0, &uiThreadID);
	}		
	unsigned uiThreadID1;
	HANDLE hThread1;
	hThread1 = (HANDLE)_beginthreadex(NULL,0,CKApplication::run_market_data,NULL,CREATE_SUSPENDED, &uiThreadID1);

	if(hThread1 == 0)
		std::cout<<"Failed to create thread"<<std::endl;
	unsigned uiThreadID2;
	HANDLE hThread2;
	hThread2 = (HANDLE)_beginthreadex(NULL,0,CKApplication::run_market_data,NULL,CREATE_SUSPENDED, &uiThreadID2);
	if(hThread2 == 0)
		std::cout<<"Failed to create thread"<<std::endl;
	ResumeThread(hThread1);
	ResumeThread(hThread2);
	Sleep(1000);
	WaitForSingleObject(hThread, INFINITE);
	WaitForSingleObject(hThread1, INFINITE);
	  while(true);
	
    return 0;
  }
  catch ( std::exception & e )
  {
    std::cout << e.what();
    return 1;
  }
}


Last edited on
You can schedule them in another thread, or change the functions themselves to sleep, then loop.
Thanks for the reply.

That's the bit confusing me. I want to run the thread 1 all the time and execute the thread 2 and 3 on specific interval by waking them up.

How can i do it so that it wont effect the thread1 functionality.

Thanks.
plss use code tags next time.

What are u using to get C++ multithreading? VS?
Last edited on
i'm using VS2010.
Been a long time since I'v used VS for C++, I mostly do C# in there.
But .net has some really good functions to help you there.

To do what you are trying I'd make a timer object, this will call a tick event every time it elapses example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public __gc class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}


You can then have your tick event call the function you need.

As for Thread 1, here you can use a ManualResetEvent. (simple explantation)This is an object that can be called by any thread, as long as they are within scope, to pass a boolean from thread to thread. What you do is in your Thread1 function make a while-loop that keeps running the code as long a the ManualResetEvent flag is set. If you then want too end the function, you only need to remove the ManualResetEvent flag.

http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent(VS.71).aspx


Since the Thread1 will be running a loop all the time, it's a good idea to implement a small sleep, like sleep(300) to prevent the thread from using 100% of you CPU core, if you are running it on a multicore this is not really needed but still advised.

And one more thing, it is always a good idea to put the thread function it's ManualResetEvent and any other data in it's own class, so you can use it multiple times as an object.
Last edited on
Hi,

Thanks for the reply. I hope this thing will help. But tell me does that mean i don't need thread 2 and 3 and just use this timer even to call those funtions created by thread 2 and 3?

More over, all this code will be included along the main..mean the timer class or i can declare it in my header files and then call the function from there.

this is a bit confusing :S

Thanks

A timer is a special thread on it's own. so this will keep running in the background and call your function, I'm not 100% sure if he calls it in the mainthread or it's own thread, I think main, but since main is not doing anything here it's fine. Thread1 will keep doing what ever it need to and your mainthread will run every 30sec to execute your function.

If you real need to do it in 2 threads, keeping the main clear, I'd use a BackgroundWorker, this is a thread but a bit less complicated and dedicated to simply running functions in the background without all the functionality and headaches of multithreading, and it can make callbacks to give feeb back on progress and so on, like the % done and so.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Hi Raggers,

Thanks for your help. It seems working now. Get back here if I got some more issues :D

Thanks again.
Topic archived. No new replies allowed.