954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Execute 2 for loops at the Very same time

If you want to run 2 loops at the Very same time in C++, will backgroundworkers have this functionality ?

An example could be how I have set up it in this code.
What I wonder is if the backgroundWorker1 first is executed and when this is finished, then backgroundWorker2 is executed.

Or is both backgroundWorker1 and backgroundWorker2 executed and working simultaniasly ?

What I really are after is to have about 10 loops working simultaniasly so any suggestions for this I will be happy to hear. The only thing that could come up in my mind is that backgroundworkers can be used for this but perheps there could be any other method for this ?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
      backgroundWorker1->RunWorkerAsync();
      backgroundWorker2->RunWorkerAsync();  
}


private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) 
{
	for( int i = 0; i < 1000000; i++	)
               {
                    //Do Stuff
               }  
}
private: System::Void backgroundWorker2_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) 
{
	for( int i = 0; i < 1000000; i++	)
               {
                    //Do Stuff
               } 	 
}
Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

You're only going to get true "at the same time" if you've really got a multi-core CPU available.

Otherwise, it's just faked by the OS by interleaving both loops very quickly.
In the same way that TV is only a rapid sequence of still images, but you perceive the result as motion.

How well the OS can manage this illusion is in part down to the nature of your "Do Stuff".

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You