Skip to main content
GameDev.net gamedev.net
🔒 Locked

Threading question

Started by MCeltic33 Jul 6, 2006 at 1:50 PM 10 replies 1.7k views
Original Post
MCeltic33
MCeltic33
I am making a program which will be the only program running on a machine (besides the OS). The program itself does not need to be multi-threaded, however, should I have the program use Sleep in a continuous loop? My reason for this is, if I do not do this, more than likely it will use 100% clock cycles for no reason, no?
Evil Steve
Evil Steve
Your program will use 100% of the CPU unless it yields to the OS through Sleep() or some blocking call like WaitForSingleObject or GetMessage().

Is your application a windows based one or a console based one? If it's a windows based one, you could use GetMessage() instead of PeekMessage() in your window processing loop to still remain responsive and use 0% of the CPU when you're not doing anything.
Driv3MeFar
Driv3MeFar
If your program is the only one running besides the OS, why do you care if you hog CPU time?
MCeltic33
MCeltic33
Quote:
Original post by Evil Steve
Your program will use 100% of the CPU unless it yields to the OS through Sleep() or some blocking call like WaitForSingleObject or GetMessage().

Is your application a windows based one or a console based one? If it's a windows based one, you could use GetMessage() instead of PeekMessage() in your window processing loop to still remain responsive and use 0% of the CPU when you're not doing anything.


It is a windows based application - would you recomend using GetMessage() in every form of the application?

Quote:
Original post by Driv3MeFar
If your program is the only one running besides the OS, why do you care if you hog CPU time?


Power consumption, heat, fan noise...
uray
uray
could you post some overview code describing your problem?, my suggestion is don't doing something when you don't need it, use some waitable object and use waitable function such as WaitForSingleObject, and when object is signaled then do the process... btw sleep() isn't cool...
Uray L. Meiviar
kordova
kordova
Quote:
Original post by uray
btw sleep() isn't cool...

Why?
Red Ant
Red Ant
Sleep( 0 ) just to yield CPU is not cool. Sleep( 2000 ) may be. That's what this guy says.
MCeltic33
MCeltic33
Quote:
Original post by Red Ant
Sleep( 0 ) just to yield CPU is not cool. Sleep( 2000 ) may be. That's what this guy says.


Interesting little read...anyone else have comments on this?
iMalc
iMalc
Quote:
Original post by Driv3MeFar
If your program is the only one running besides the OS, why do you care if you hog CPU time?
Because it uses a lot more power, the fan has to go faster, and the battery time of a laptop under these conditions it dramatically shortened.

You should use a sleep(1). Don't worry about anyone telling you that it might sleep for 15ms on the odd occasion. Windows is not a RTOS anyway, and you shouldn't need it to be any more accurate.
JohnBolton
JohnBolton
Sleep is generally a bad idea. It is better to wait for vblank (D3DPRESENT_INTERVAL_DEFAULT or D3DPRESENT_INTERVAL_ONE). That way your program doesn't waste time (and power) drawing frames that can't be seen anyway.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Evil Steve
Evil Steve
Quote:
Original post by MCeltic33
Quote:
Original post by Evil Steve
Your program will use 100% of the CPU unless it yields to the OS through Sleep() or some blocking call like WaitForSingleObject or GetMessage().

Is your application a windows based one or a console based one? If it's a windows based one, you could use GetMessage() instead of PeekMessage() in your window processing loop to still remain responsive and use 0% of the CPU when you're not doing anything.


It is a windows based application - would you recomend using GetMessage() in every form of the application?
Unless you need to do other idle processing that is non-blocking, yes.

Topic Locked

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

Sign in to reply to this topic.