huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11920

Simple threads workin in an order

My VC++ 2010 express install time has expired so i cant use it anymore without changing the date from right-down. I dont want to do this so i am asking you these questions:

Do you think, below multi-threaded program's threads working in an order?

What if i put similar 3 more threads to calculate next-1000 particles, does the contet-switches become exhausting or just the Sleep(10)s are exhausting?

Note: Each calculation(each one of force,vel., pos.) takes about 9 ms.

Something like this:
core1:first 1000 particles forces     //
core2:first 1000 particles velocities //===>these 3 are connected
core3:first 1000 particles positions  // ------------------------------
                                                                      |
core4:next 1000 particles forces      //                               ====these 2 will be connected
core5:next 1000 particles velocities  //===>these 3 are connected     |
core6:next 1000 particles positions   // ------------------------------

boolean locker1;
boolean locker2;
boolean locker3;

boolean worker1;
boolean worker2;
boolean worker3;

void core1(void * x)
{
    while(worker1)
    {
         while(!locker1){Sleep(10);}
         for(int i=0;i<1000;i++)
         {
              //calculate 1000 particles forces
         }
         locker2=true; //starts core2 thread
         while(locker2){Sleep(10);} //core2 must be working
         while(locker3){Sleep(10);} //core3 must be working
    }
   _endthread();
}

void core2(void * y)
{
    while(worker2)
    {
         if(!locker2){Sleep(10);}
         for(int i=0;i<1000;i++)
         {
              //calculate 1000 particles velocities
         }
         locker3=true; //starts core3 thread
         while(locker3){Sleep(10);} //core3 must be working
         while(locker1){Sleep(10);} //core1 must be working
    }
   _endthread();

}

void core3(void * z)
{
    while(worker3)
    {
         if(!locker3){Sleep(10);}
         for(int i=0;i<1000;i++)
         {
              //calculate 1000 particles positions
         }
         locker1=true; //starts core1 thread
         while(locker1){Sleep(10);} //core1 must be working
         while(locker2){Sleep(10);} //core2 must be working
    }
   _endthread();

}

int main()
{
    locker1=false; 
    locker2=false;
    locker3=false;
    worker1=true; 
    worker2=true;
    worker3=true;
    _beginthread(core1,0,(void *)0);
    _beginthread(core2,0,(void *)0);
    _beginthread(core3,0,(void *)0);

    locker1=true; //gets the waiting core1-thread working
                  //so i think when it finishes, it releases core2 to work
                  //when core2 finishes, core3 starts working

    Sleep(100);
    worker1=false;
    worker2=false;
    worker3=false; //after a while i shut them down

}

Please if any one having VC++ could give me some hints or suggestions i appreciate.

Or should i just forget the 3+3-->2 system and do this(6)? :

core1 first 233 particles for computing forces ----->all for velocity ----->all for psoition
core2 next 233 particles for computing forces ----->all for velocity ----->all for psoition
core3 next 233 particles for computing forces ----->all for velocity ----->all for psoition
core4 next 233 particles for computing forces ----->all for velocity ----->all for psoition
core5 next 233 particles for computing forces ----->all for velocity ----->all for psoition
core6 last 233 particles for computing forces ----->all for velocity ----->all for psoition

and just wait all of them finish to get to next calculation?

Upvotes: 0

Views: 116

Answers (3)

Blastfurnace
Blastfurnace

Reputation: 18652

If you are interested in adding parallelism to your C++ program and are using Visual C++ can I suggest you look at Microsoft's Concurrency Runtime and especially the Parallel Patterns Library (PPL). These make it incredibly simple to do parallel work without grubbing around in the low-level details.

Intel's extensive Threaded Building Blocks is another option to look into.

Upvotes: 1

Elemental
Elemental

Reputation: 7521

I really don't see the advantage of threads in this instance the idea is to get them to run together, your locker variables seem to be designed to ensure at any given time 2 of the three are in sleep. If so then why not just run them in the same thread sequentially. It seems to me that these calculations are dependant on each other and as such no parallel threading is going to help you here.

Upvotes: 1

MSalters
MSalters

Reputation: 179907

"Do you think, below multi-threaded program's threads working in an order?"

Not at all. You violate way too many rules on multi-threaded programming.

Also, the point of threads is to execute things in parallel. If you need to do A, B and C in sequence, do them in the same thread.

Upvotes: 2

Related Questions