我的 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?