Jump to content
  • Advertisement
Sign in to follow this  
MCeltic33

Threading question

This topic is 4340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!