Create a delay without using Sleep

This is a discussion on Create a delay without using Sleep within the C++ Programming forums, part of the General Programming Boards category; Its almost exam time and my teacher would like us to learn how to create a delay in our programs ...

Create a delay without using Sleep

Its almost exam time and my teacher would like us to learn how to create a delay in our programs without using the Sleep function in the windows.h library. He said that we might do somthing like create a loop where there is a counter that will count to a very high number or somthing like that. I tried somthing like this:

There seemed to be no diference between the two. Any suggestions? Thanks

BTW: My teacher said that sleep isn't a good function in most cases because it makes the system inactive for that time and no input will do anything, a function that can still retrieve input while pausing would be helpfull aswell.

If you are not getting any differences, maybe make your loops longer (or nest them, or use a double and increment by 0.001 or something small). The reason is that the computer is very fast, and 100000 runs through a simple loop is very quick.

Busy loops are a poor way of creating delays; they tie up CPU resources (in a multi-programming environment), and depend on the speed of the CPU. The counter-loop based delay ties up your program until it exits, and uses a lot of CPU cycles that could be used for other processes.

There are standard ways to create somewhat predictable delays.

With Windows (when using an event loop), you set up a timer event and then wait until you get the timer event before proceeding with your current code (though you should process all of the events that occur; they should not relate to the part of the program you want the delay in). This, of course, only works with one OS (though the same concept is applicable in many graphical environments.)

For POSIX environments, and for console apps, another method is to use the alarm(2) system call and set up a signal handler for the event (SIGALRM) that's triggered when the alarm fires off.

The standard POSIX library call sleep(3) does not block the OS from doing other things, just the process that calls it. This may not match the Windows sleep function you mentioned in your posting.

It's been about 6 years since I did any serious programming on Windows, so my resources and memory on that environment are a bit scarce at the moment.

if you are going to go by way of the loop, empty loops will iterate remarkably fast even if the value is large. the argument would be that this is a very CPU intensive delay, but if you want to have a loop based delay that doesnt require 100 million iterations, try a loop that prints white space to the console every iteration - really slows it down.

Code:

int i;
for (i=0; i<100000; i++)
{
printf (" ");
}

i have a 3Ghz processor and that code ran for about 3 seconds - can easily be increased by increasing the number of iterations. also, cout is marginally slower than printf - something i saw in the faq once.

there is yet another method which i have used, and it allows you to specify in seconds roughly how long you want to delay - you will need time.h.

Its almost exam time and my teacher would like us to learn how to create a delay in our programs without using the Sleep function in the windows.h library. He said that we might do somthing like create a loop where there is a counter that will count to a very high number or somthing like that.
BTW: My teacher said that sleep isn't a good function in most cases because it makes the system inactive for that time and no input will do anything, a function that can still retrieve input while pausing would be helpfull aswell.

your teacher sounds like an idiot. Sleep does not "make the system inactive", it yields your program to the kernel for a specified length of time (on most windows platforms, it's at least 10-15ms to allow for a thread switch).

As for using an empty loop as a delay, any decent compiler will optimise it out of the program.

if you are going to go by way of the loop, empty loops will iterate remarkably fast even if the value is large. the argument would be that this is a very CPU intensive delay, but if you want to have a loop based delay that doesnt require 100 million iterations, try a loop that prints white space to the console every iteration - really slows it down.

Code:

int i;
for (i=0; i<100000; i++)
{
printf (" ");
}

i have a 3Ghz processor and that code ran for about 3 seconds - can easily be increased by increasing the number of iterations. also, cout is marginally slower than printf - something i saw in the faq once.

Although something I've noticed when having great big chunks of output statements (using cout), the output lags behind the actual execution, so while the output is still at the x repeat, the actual program is up to 2x or whatever.

That was using the gcc compiler that came with my powerbook, osx 10.2.8, so maybe my computer's just screwed up.

I think what he meant by this

My teacher said that sleep isn't a good function in most cases because it makes the system inactive for that time and no input will do anything, a function that can still retrieve input while pausing would be helpfull aswell.

Is that the program won't process input during that time, so he wants something that'll pause the program before executing an event, but will still take input and execute other program commands.

You could also use the windows function WaitForSingleObjectEx. It puts the current thread to sleep much like sleep does, except that if the handle given to it was created as an event (with CreateEvent()), the sleep can be woken up by signaling the event from another thread (using SetEvent()).

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

Dev-C++ supported inline assembly when I stopped using it, and I know MSVC++ does as well, just not with that sytanx. For VS 2003:

Code:

_asm
{
}

On Dev-C++ I remember it used AT&T syntax unless you set a compiler switch, though it had some issues if you tried to use intel syntax if I recall correctly. Come to think of it, pretty much everything about using inline asm in Dev was a pain, especially the me-not-knowing-assembly part