Jump to content
  • Advertisement
Sign in to follow this  
Idov

Making a "Busy Loop" not consume that much CPU (without Sleep)

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

Hi!

I want to measure a very small period of time ( let's say 0.2 ms).

In order to do this, I'm using busy loop which checks whether or not we waited in the busy loop for the requested time like this:

 

double startTime = MeasureTime();

double endTime = MeasureTime();

while (endTime - startTime < timeToWait)
{

     endTime = MeasureTime();
}

 

 

I'd like to use some functions which don't use much CPU in that loop in order to ease the CPU load.

which functions should I use?

 

thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Guest Hiwas

While yield is a valid way to reduce the load, it is still effectively a sleep by a different name.  (On windows it is usually just a Sleep( 0 ) call anyway.)  Another option which won't reduce the apparent CPU load, but does in fact basically idle the CPU is to use the assembly instruction 'pause'.  The instruction is used in user space spin locks and basically issues a uop which does nothing except wait for the CPU pipeline to be empty.  Place an inline assembly instruction of this within a loop and the cpu will effectively be doing an increment and compare (for the loop) and then waiting for the pipeline to flush before doing anything else.  This still shows up as busy waiting in performance monitoring but there is effectively no load and in the case of hyperthreading, the other hardware thread is at nearly full utilization of the core while this is happening.

Share this post


Link to post
Share on other sites

Win32 threads: Sleep(0)

No God, NO!

Use SwitchToThread to yield (supported since Win XP)

Sleep( 0 ) is a terrible way of yielding. If you're looking to avoid consuming CPU cycles (i.e. lower power usage) prefer Sleep( 1 ) over Sleep( 0 )

Share this post


Link to post
Share on other sites

While yield is a valid way to reduce the load, it is still effectively a sleep by a different name.  (On windows it is usually just a Sleep( 0 ) call anyway.)

Sleep(0) on Windows is treated as a special case. I'm not sure I'd call yield "sleep by a different name," though, because they have some important differences.

 

Win32 threads: Sleep(0)

No God, NO!

Use SwitchToThread to yield (supported since Win XP)

Sleep( 0 ) is a terrible way of yielding. If you're looking to avoid consuming CPU cycles (i.e. lower power usage) prefer Sleep( 1 ) over Sleep( 0 )

The OP wants to sleep for 0.2 milliseconds... Sleep(1) will overshoot that by a ton. SwitchToThread() is probably preferable (thanks for pointing it out to me!), but I can't see any significant difference between it and Sleep(0) in the MSDN docs. Maybe I'm missing something.

Share this post


Link to post
Share on other sites

Ok, thanks!
But as far as I understand Sleep(0), SwitchToThread, etc. will only switch to another thread if there is another thread waiting to use the CPU, but if there isn't one - it won't do anything.
 

I will try to use it, but what do you say about using functions such as printf() that uses the IO devices and not the CPU in order to achieve my goal? 

Share this post


Link to post
Share on other sites

Ok, thanks!
But as far as I understand Sleep(0), SwitchToThread, etc. will only switch to another thread if there is another thread waiting to use the CPU, but if there isn't one - it won't do anything.

That's true, yielding typically only helps if other threads are available for running. But remember, on modern computers there are often hundreds of threads in existence, and the OS can idle the CPU in its own threads if there isn't work to be done (so the OS threads can potentially eat the remaning time with proper CPU idling). I recall a few times I had a program doing a loop that went from about 100% CPU usage to near 0% usage after putting a single yield in it (I don't recall the exact amount it dropped by, but it was significant). [Edit: I've been thinking about this last statement for the past few days and I'm actually second guessing myself; it's possible I did a sleep(1) instead of an actual yield]

 

I will try to use it, but what do you say about using functions such as printf() that uses the IO devices and not the CPU in order to achieve my goal? 

Blocking on IO devices might work, but it's a pretty uncommon way of doing things. Also, once you get in the sub-millisecond range (like you are), blocking on IO devices might block for longer than you want.

Edited by Cornstalks

Share this post


Link to post
Share on other sites

But as far as I understand Sleep(0), SwitchToThread, etc. will only switch to another thread if there is another thread waiting to use the CPU, but if there isn't one - it won't do anything.

Unless you're writing code for an isolated system, there is almost always another thread/task waiting to use the cpu.

 

As far as I know you will not get the desired effect from the loop, since most system timers are no more precise than ~10 ms, even though you can get the number represented in nanoseconds. So unless you are using a method to get a very accurate time, you might as well use sleep.

 

Depending on what you are doing I would recommend you to change the flow of the program, such that you don't have to use a loop to wait for 0.2 ms, rather wait until a certain condition set elsewhere in your code is true. Is this just a theoretical question or are you working on something in particular?

Share this post


Link to post
Share on other sites
One of the better options for a short wait is to use the WaitableTimer object. It can be set in (approximately) 200 nanosecond increments, then use WaitForSingleObject (or similar) to wait for it to trigger. There is more on MSDN. Be sure to pass a NEGATIVE time value so it knows the value is relative, otherwise you'll be setting timers that trigger back in the year 1600 or so.

Ultimately, even this solution isn't guaranteed. Windows simply is not a real time operating system. With a waitable timer you will probably get woken up about when you expect, but your thread can be suspended at any time, and the granularity for resuming threads is roughly 10ms. This is just a fact we get to live with.

Share this post


Link to post
Share on other sites

 VildNinja:

As far as I know you will not get the desired effect from the loop, since most system timers are no more precise than ~10 ms, even though you can get the number represented in nanoseconds. So unless you are using a method to get a very accurate time, you might as well use sleep.

 

I'm using QueryPerformanceCounter in order to measure time in a higher resolution. :)


CornStalks:

That's true, yielding typically only helps if other threads are available for running. But remember, on modern computers there are often hundreds of threads in existence, and the OS can idle the CPU in its own threads if there isn't work to be done (so the OS threads can potentially eat the remaning time with proper CPU idling). I recall a few times I had a program doing a loop that went from about 100% CPU usage to near 0% usage after putting a single yield in it (I don't recall the exact amount it dropped by, but it was significant).

 

I just tried using Sleep(0). It didn't have any effect :/


Frob:

One of the bester options for a short wait is to use the WaitableTimer object. It can be set in (approximately) 200 nanosecond increments, then use WaitForSingleObject (or similar) to wait for it to trigger. There is more on MSDN. Be sure to pass a NEGATIVE time value so it knows the value is relative, otherwise you'll be setting timers that trigger back in the year 1600 or so.


I will sure try it, but how is it possible?
I've always been told that windows is not a RT system and it can't wait for periods shorter than 1 ms... Is it wrong?

 

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!