Threading question

Started by
10 comments, last by Evil Steve 17 years, 9 months ago
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?
Advertisement
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.
If your program is the only one running besides the OS, why do you care if you hog CPU time?
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...
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
Quote:Original post by uray
btw sleep() isn't cool...

Why?
Sleep( 0 ) just to yield CPU is not cool. Sleep( 2000 ) may be. That's what this guy says.
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?
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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!

This topic is closed to new replies.

Advertisement