| Thread Closed |
Time Delay in C++. |
Share Thread | Thread Tools |
| Jan21-06, 11:21 AM | #1 |
|
|
Time Delay in C++.
Hello. I'm wondering how to make a time delay of a few seconds using C++. The simplest way that came up to my mind is just to make loop for a few millions time or so, but the delay would change from computer to computer.
|
| Jan21-06, 11:26 AM | #2 |
|
|
You could simply use the ordinary C time functions. See "time" and "clock" in the "ctime" header.
|
| Jan21-06, 11:42 AM | #3 |
|
|
Wait approximately 5 seconds with something like this:
for (time_t t = time() + 5; time() < t; ) {} If you need more accuracy, use clock_t and clock() instead, along with the CLK_TCK value. If you are programming for Windows, use Sleep(5000) to let the process sleep for 5000 milliseconds without wasting all those CPU cycles. For other OSes, check the documentation for a similar function. |
| Jan21-06, 02:52 PM | #4 |
|
Recognitions:
|
Time Delay in C++.
sleep(<time in ms>) is the standard way of doing it.
|
| Jan21-06, 04:06 PM | #5 |
|
|
Are those functions in C or C++? If the latter, what library should I include?
|
| Jan21-06, 04:15 PM | #6 |
|
Recognitions:
|
Under Unix sleep is in unistd.h. I'm not sure about windows.
|
| Jan21-06, 04:24 PM | #7 |
|
|
No such library in windows. There is a time.h windows though, but no sleep function. Is there a way to view the source file?
|
| Jan21-06, 04:30 PM | #8 |
|
|
error C2660: 'time' : function does not take 0 parameters |
| Jan21-06, 04:38 PM | #9 |
|
|
I found this piece of code on the net:
time_t start_time, cur_time; time(&start_time); do { time(&cur_time); } while((cur_time - start_time) < n); where n is the number of seconds. when i run my programme, it first does the delay, then run the rest. i mean that the test programme should cout my first name, delay 3 seconds and the output my last name, but it delays 3 seconds then outputs my full name. weird. |
| Jan21-06, 04:44 PM | #10 |
|
Recognitions:
|
Ugh, I did a google search and it said windows.h
|
| Jan21-06, 04:49 PM | #11 |
|
|
void sleep( clock_t wait )
{ clock_t goal; goal = wait + clock(); while( goal > clock() ) ; } Found the sleep function on the net, but still, the programme does the delay, then outputs the full name. *shrugs* |
| Jan21-06, 04:55 PM | #12 |
|
|
But are you using Unix or Windows? Because #include <windows.h> with Sleep(milliseconds) in Windows or #include <unistd.h> with sleep(milliseconds) in Unix is simpler and more reliable. The sleep function you show that uses clock_t instead of time_t does not count in seconds or milliseconds but in system clock ticks. This varies from system to system so the pause duration is not consistent across systems (if consistency matters). |
| Jan21-06, 05:06 PM | #13 |
|
|
I have both windows and linux, but i usually programme on windows since i'm new to linux. Flushing the buffer did take care of the problem. Thanks guys!
|
| Jan21-06, 07:10 PM | #14 |
|
|
|
| Jan23-06, 12:58 PM | #15 |
|
|
Edit: the C standard does not require any fine granularity for clock(). It has to "tick" at least once per second, even if CLOCKS_PER_SEC is 1000000. So if you choose 50 ms depending on the implementation of C/C++ you may still get 1 full second.
Sleep(DWORD milliseconds) is the Windows api call you want, in windows.h Consider using it. Implementing a roll-your-own time delay means the time delay will not port - and this may mean going from Windows XP on a PIII 500MHz to Windows XP on a P4 3.2GHz will cause the code not to behave correctly. Plus, turning on optimization may allow a compiler optimize away a few of these kinds of homegrown loops Use the api - all time delay calls are hardware/OS dependent anyway, so you are not losing portability. You are probably gaining some. |
| Jan23-06, 08:26 PM | #16 |
|
|
And if you really want, demarcate a platform-dependent block of your source code, and define your own "my_sleep" which is merely a wrapper for whatever the best method of sleeping is on a given system.
(So, when porting, the only code that ought to need changing is the code in this block) |
| Mar8-07, 06:00 PM | #17 |
|
|
You can simply use the Sleep(milliseconds) function which must have windows.h included...
I'm a little late, but just in case you still check it a year after the fact :) Example: #include <windows.h> Sleep(1000); //this will sleep for 1 second (1000) milliseconds //It of course needs to be put into some function, but I'm a smidge too lazy :) |
| Thread Closed |
| Thread Tools | |
Similar Threads for: Time Delay in C++.
|
||||
| Thread | Forum | Replies | ||
| Assembly language time delay | Electrical Engineering | 10 | ||
| Shapiro time delay? | Special & General Relativity | 42 | ||
| Time delay | Electrical Engineering | 6 | ||
| Time delay problem for project... | Programming & Comp Sci | 1 | ||
| time delay equations | Introductory Physics Homework | 4 | ||