You are here:

C++/C++ delay a loop or funtion for paticular time delay

Advertisement


Question
i need to delay my loop and execute it so how to do it in C++

Answer
I am not sure I fully understand what you are asking so I shall say sorry now if I have misunderstood what you wish to know.

As far as I can tell you wish to delay the iterations of a loop or return (?) from a function thus:

       While condition is true:
         Do stuff
         Wait for required time
       End loop

Or for a function:

       Function func()
         Do Stuff
         Wait for required time
         Return value
       End Function

The most obvious way is to loop until the required time has passed. The C++ standard library contains time functions (inherited from the C standard library). Of interest here is a function called clock which is declared in the header <ctime> or for C <time.h>. clock returns the processor time used since the beginning of the program execution, in units of ticks the number of ticks per second being defined by CLK_TCK (from my original ANSI C (C90) reference) or CLOCKS_PER_SEC (from my C99 reference). Note that the C++ standard references the original C90 (or it may have been C89) standard which I have not managed to get hold of!

Anyways, to wait for 1 second you could do the following:

       clock_t start_tick( clock() );
       while ( clock() start_tick < CLK_TCK )
         ; // do nothing but loop...

clock_t is most likely just an alias for some suitably sized integer type.

I tried the above using Visual C++ 7.1 and it accepted both CLK_TCK and CLOCKS_PER_TICK.

The above method of spinning in a tight loop is as far as I am aware the only way to delay a program without recourse to operating system functions. It is also the most stupid way to delay on a modern multitasking/multiprocessing operating system as you are tying up the processor needlessly. Such operating systems have a way to wait by suspending your application's process or a process thread and allowing the processor to do something else until the requested delay has elapsed. Note that the time delayed may in fact be longer than requested as the task (process or thread) becomes eligible for execution scheduling after the specified delay but other tasks (processes or threads) may have a higher priority and so your task may have to wait until they yield or complete their time slot. On UN*X and other POSIX like operating systems the functions are sleep or nanosleep sleep takes a wait time in seconds an nanosleep in seconds and nanoseconds. MS Win32 based operating systems (Windows 95, 98, ME, NT, 2000, XP etc..) have a function called Sleep which sits in the middle as far as the resolution is concerned taking the number of milliseconds to wait as a parameter.

So on a Win32 system you could do something like the following:
 
       while (not_done)
       {
         not_done = DoStuff();
         Sleep(20); // Suspend thread execution for 20ms
       }

You can look up the required headers and libraries using the internet the Win32 API documentation can be found in the MSDN library which is online as part of the MSDN site at http://www.msdn.microsoft.com/. Linux documentation for sleep can be found at http://www.die.net/doc/linux/man/man3/sleep.3.html and nanosleep at http://www.die.net/doc/linux/man/man2/nanosleep.2.html.

Hope this was of some use.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.