I'm trying to use a while function inside a pthread and have the problem that my processor load of both processors reaches 100% as long as the while function is working (even an empty one needs 100% load so it's not the content of the function?!)
while(ende==0){
moveplayerp=my_data->moveplayerp;
//#################################
//########## poschecks ############
//#################################
// Angriff oder VERT?
if(ballteam==playerteam){
// offensiver spielen
if(side==1){
// spielen von links nach rechts
if(pos==1){
xmax=16;
xmin=0;
}
elseif(pos==2){
xmin=25;
xmax=50;
}
elseif(pos==3){
xmin=40;
xmax=80;
}
elseif(pos==4){
xmin=75;
xmax=100;
}
}
}else{
// defensiv spielen
if(side==1){
// spielen von links nach rechts
if(pos==1){
xmax=16;
xmin=0;
}
elseif(pos==2){
xmin=0;
xmax=40;
}
elseif(pos==3){
xmin=25;
xmax=60;
}
elseif(pos==4){
xmin=50;
xmax=75;
}
}
}
// Ausrichtungen
// Taktische einstellungen
// xmin/xmax festlegen
my_data->xmin=xmin;
my_data->xmax=xmax;
// Mitspieler
// hier bin ich
// hier geht's hin (variabeln festlegen)
/*if(moveplayerp==0){
xold=my_data->posx;
yold=my_data->posy;
x = my_data->movetox;
y = my_data->movetoy;
x=ballx;
y=bally;
dist=sqrt((x-xold)*(x-xold) + (y-yold)*(y-yold));
if(dist!=0){
moveplayer(threadarg);
}
}*/
}
"ende" is a global var which will be changed to another value if some conditions are true (this is working out).
Has anyone an idea why my processor load reaches 100%?
Greetings
Marco
That's usual case when you code games (other apps) with plain loop, it runs as fast as it can so at 100%... resulting even in 10000's frames per second... there's nothing wrong with while, just limit FPS to any number >= 60, maybe >=200 or so... this would also make your app running same speed on all machines, if they are able to display given number of FPS... if your app seems that it cannot (you can easily detect that you render less frames than wanted in one second) you can dynamically lower FPS...
If you do all the drawing in other thread, think about making loop sleep for let's say 1ms every time. Usually events from libraries are processed at speed of 1-10ms (at least that's case for SDL on some system's etc) so you wouldn't notice even single slowdown, but cpu usage should go down dramatically.
Hi Andrej
As you recognized right it's a part of a game calculation (basically doing the background work). Although there's no output (yet) it's working fine when i let the function sleep for 1 ms. Thanks a lot, the program uses now close to 0% of the processor load.