Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Delay Loop (http://www.programmingforums.org/showthread.php?t=1316)

mswift Nov 28th, 2004 12:57 PM

I am using a book called Teach yourself C++ Third Edition.
On Exercise #4 on page 38 it calls for a "delay loop" to be used. The purpuse of the exercise is to demonstrate function overloading, something I understand, but what is a delay loop?

Eggbert Nov 28th, 2004 1:03 PM

>what is a delay loop?
:

for ( int i = 0; i < SOME_BIG_NUMBER; i++ )
 ; // Eat up CPU cycles

This is far from the best way to implement a delay, but it should work for your purposes.

kurifu Nov 28th, 2004 4:14 PM

Actually it does not mean that you have to eat up CPU cycles in a delay loop... is just means that there is code in there that is causing a significant delay between iterations, most likely from a sleep command which forces the proecessor to not do anything for a specified period of time in milliseconds.

:

for(;; )
{
  ...execute some code...
  Sleep( 100 );
}


is a good example of a delay loop. Delay loops are very commonly used in multi-threading and server-client applications, as well as many other applications. Sometimes you can have a process trying to pol for input from a network socket or a local device, the problem is your processor will run a polling loop as fast as it can unless you forece it to slow down... you do this with sleep.

On another interesting not... Sleep( 0 ) is usually a valid statement, and it does not execute a CPU wait for 0 milliseconds, what this usually does is it instructs the operating system to end the timeslice for the process that is running and execute other timeslices for other processes and threads before returning, this is typically a very effective way of managing the polling loop issue.


All times are GMT -5. The time now is 2:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC