我的VC ++ 2010快速安装时间已过期,因此我无法再使用它而无需从右下方更改日期。我不想这样做,所以我问你这些问题:
您认为,在多线程程序的线程中处理订单吗?
如果我将类似的3个线程用于计算下一个1000粒子,那么背景开关是否会变得疲惫,或者只是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?
答案 0 :(得分:2)
“你认为,在多线程程序的线程中按顺序工作吗?”
完全没有。你违反了多线程编程的太多规则。
此外,线程的要点是并行执行。如果您需要按顺序执行A,B和C,请在同一个线程中执行它们。
答案 1 :(得分:1)
我真的没有看到线程的优势在这个例子中,想法是让它们一起运行,你的锁定变量似乎被设计为确保在任何给定时间三个中的2个处于睡眠状态。如果是这样,那么为什么不按顺序在同一个线程中运行它们。在我看来,这些计算是相互依赖的,因此没有平行线程可以帮助你。
答案 2 :(得分:1)
如果您有兴趣在C ++程序中添加并行性并使用Visual C ++,我建议您查看Microsoft的Concurrency Runtime,尤其是Parallel Patterns Library (PPL)。这些使得并行工作变得异常简单,而不会在低级细节中进行讨论。
英特尔广泛的Threaded Building Blocks是另一种选择。