Start New Discussion within our Software Development Community

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
               } 	 
}

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".

This article has been dead for over six months. Start a new discussion instead.