0

我的 VC++ 2010 快速安装时间已过期,因此如果不从右下更改日期,我将无法再使用它。我不想这样做,所以我问你这些问题:

你认为,下面的多线程程序的线程是按顺序工作的吗?

如果我再放置类似的 3 个线程来计算下一个 1000 个粒子,contet-switches 会变得筋疲力尽还是只是 Sleep(10) 筋疲力尽?

注意:每次计算(force,vel.,pos.)大约需要 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

}

如果有 VC++ 的人能给我一些提示或建议,我将不胜感激。

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?
4

3 回答 3

2

“你认为,多线程程序下面的线程是按顺序工作的吗?”

一点也不。您违反了太多关于多线程编程的规则。

此外,线程的重点是并行执行事物。如果您需要依次执行 A、B 和 C,请在同一个线程中执行。

于 2012-09-03T07:08:37.397 回答
1

在这种情况下,我真的看不到线程的优势,这个想法是让它们一起运行,您的储物柜变量似乎被设计为确保在任何给定时间三个中的两个处于睡眠状态。如果是这样,那么为什么不按顺序在同一个线程中运行它们。在我看来,这些计算是相互依赖的,因此没有并行线程可以帮助你。

于 2012-09-03T07:12:09.313 回答
1

如果您有兴趣在 C++ 程序中添加并行性并且正在使用 Visual C++,我建议您查看 Microsoft 的并发运行时,尤其是Parallel Patterns Library (PPL)。这些使得并行工作变得非常简单,而无需在低级细节中摸索。

英特尔广泛的线程构建模块是另一个值得研究的选择。

于 2012-09-03T07:24:31.890 回答