Skip to main content
gamedev.net
🔒 Locked

100% CPU infinite loop

Started by subflood 9 replies 1.3k views
Original Post
subflood
subflood
What can I do in an infinite loop to make sure it doesn't use up all of my CPU unless it needs to.
Agony
Agony
Assuming Win32, the easiest thing is to call Sleep(n), where n is the number of milliseconds that you want to sleep. Sleep(0) will allow other things to run if they want to, but will use all of the CPU power in the absence of other programs. (me is slow)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
cignox1
cignox1
I suppose that your goal is something else: it would be easier for someone to answer if you describe your problem. I.e, should it be the main loop for an application? Which language? If you can set timers with callback functions, this could sometimes be a solution, or perhaps calling once a while sleep() in some enviroments (don't know really if it would save the cpu).

EDIT: ah, sorry, the replies were not displayed at first, so I thought there was none.
intransigent-seal
intransigent-seal
[assuming windows]

Another thing to do is that if you aren't going to be doing anything in your loop (eg, if your game has been minimized, and you're automatically paused it so you don't need to do any processing), use GetMessage instead of PeekMessage. GetMessage will let your application go to sleep until it gets sent a message, so the CPU can be given to other apps.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Sneftel
Sneftel
Bad:
while(true){    if(is_something_to_do())    {        do_something();    }}

Better:
while(true){    sleep_until_something_to_do();    do_something();}

remmargorp
remmargorp
Quote:
Original post by Sneftel
Bad:
while(true){    if(is_something_to_do())    {        do_something();    }}

Better:
while(true){    sleep_until_something_to_do();    do_something();}



If I was on my PC I would draw up a parody of the Animaniacs "Good Idea, Bad Idea"
subflood
subflood
It was just to stop the CPU from going to 100% when the windows is not focused.

thanks all
JohnBolton
JohnBolton
Quote:
Original post by subflood
It was just to stop the CPU from going to 100% when the windows is not focused.


In that case, you should look for WM_ACTIVATE messages and act appropriately. You might also want call IsIconic() to find out if the window is minimized or not.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
subflood
subflood
Quote:
Original post by JohnBolton
Quote:
Original post by subflood
It was just to stop the CPU from going to 100% when the windows is not focused.


In that case, you should look for WM_ACTIVATE messages and act appropriately. You might also want call IsIconic() to find out if the window is minimized or not.


I'm using SDL


ZQJ
ZQJ
As far as I remember SDLs timing capabilities are pretty crap. Quake used WaitForMultipleObjects to do its sleeping.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.