Jump to content
  • Advertisement
Sign in to follow this  
Drethon

Releasing the CPU to the system

This topic is 4439 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

How does one release the CPU for the system to use so that a program isn't continuously using 100% CPU. This is also to double as a timeclock in the main loop. I'm in windows development with Visual Studio but will be eventually be moving to Linux.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by smart_idiot
You can call them with some tiny value, probably even 0.


If you pass zero to Sleep, it will immediately return, provided that there are no threads of equal priority running.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drethon
This is also to double as a timeclock in the main loop.


Don't use Sleep as a timeclock. The number of milliseconds you pass in to Sleep is a suggestion to the OS, it's not even remotely a precise number.

Sleep is a good way to yeild but if you want timing look into QueryPerformanceCounter. [Edited for truth =)]
-me

Share this post


Link to post
Share on other sites
In regards to usleep I just read:
      This function is obsolete. Use nanosleep or setitimer instead.
so feel free to assume I don't know what I'm talking about.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
QueryPerformanceTimer.

You mean QueryPerformanceCounter :)

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
If you pass zero to Sleep, it will immediately return, provided that there are no threads of equal priority running.


I believe you mean "...of equal priority ready to run".

Share this post


Link to post
Share on other sites
Quote:
Original post by bakery2k1
Quote:
Original post by raz0r
If you pass zero to Sleep, it will immediately return, provided that there are no threads of equal priority running.


I believe you mean "...of equal priority ready to run".


Yes :) - Thanks for the correction!

Share this post


Link to post
Share on other sites
If you're doing Windows programming using GUIs (Not a console app), then if you use GetMessage() in your main message loop, it'll free all the time to the OS until you get a message.

As for Sleep(), the thread will yield for at least the number of miliseconds specified, but it could be longer.

So, if you're using a "real" Win32 app, use GetMessage(), otherwise use Sleep() with a small delay (I'd say 0-100ms, depending on how responsive you need your app)

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!