How to add a time delay to a C ++ program?

I am trying to add a time delay in a C ++ program and was wondering if anyone has any suggestions on what I can try or the information I can look at?

I would like to receive more detailed information on how I am implementing this time delay, but until I have additional information on how to add a delay with a delay, I am not sure how I should try to implement this.

+45
c ++ time
01 Oct '08 at 16:46
source share
13 answers

Win32: Sleep(milliseconds) is what you

unix: usleep(microseconds) is what you want.

sleep () only takes a few seconds, which are often too long.

+48
Oct 01 '08 at 16:52
source share

Updated answer for C ++ 11:

Use the sleep_for and sleep_until :

 #include <chrono> #include <thread> int main() { using namespace std::this_thread; // sleep_for, sleep_until using namespace std::chrono; // nanoseconds, system_clock, seconds sleep_for(nanoseconds(10)); sleep_until(system_clock::now() + seconds(1)); } 

With these functions, you no longer need to constantly add new functions for better resolution: sleep , usleep , nanosleep , etc. sleep_for and sleep_until are template functions that can take any resolution values โ€‹โ€‹through chrono types; hours, seconds, femtoseconds, etc.

In C ++ 14, you can further simplify code with literal suffixes for nanoseconds and seconds :

 #include <chrono> #include <thread> int main() { using namespace std::this_thread; // sleep_for, sleep_until using namespace std::chrono_literals; // ns, us, ms, s, h, etc. using std::chrono::system_clock; sleep_for(10ns); sleep_until(system_clock::now() + 1s); } 

Please note that the actual duration of sleep depends on the implementation: you can ask to sleep for 10 nanoseconds, but the implementation may end up sleeping for a millisecond instead, if it is the shortest thing that it can do.

+62
Mar 17 2018-12-12T00:
source share
 #include <unistd.h> usleep(3000000); 

It will also sleep for three seconds. You can refine the numbers a bit, though.

+19
01 Oct '08 at 16:50
source share

Please note that this does not guarantee that the time during which the thread will sleep will be somewhere close to the waiting period, it only guarantees that the time until the thread continues to execute will be at least desired. The actual delay will vary depending on the circumstances (especially the load on the machine in question) and may be orders of magnitude higher than the desired sleep time.

In addition, you do not indicate why you need to sleep, but usually you should avoid using delays as a synchronization method.

+8
01 Oct '08 at 16:55
source share

Do you want something as simple as

 sleep(3); 
+5
01 Oct '08 at 16:47
source share

You can also use select (2) if you want microsecond accuracy (this works on a platform that does not have usleep (3))

The following code will wait 1.5 seconds:

 #include <sys/select.h> #include <sys/time.h> #include <unistd.h>` int main() { struct timeval t; t.tv_sec = 1; t.tv_usec = 500000; select(0, NULL, NULL, NULL, &t); } 

`

+3
01 Oct '08 at 16:49
source share

Yes, sleep is probably the function of choice here. Note that the time elapsed into the function is the least amount of time that the calling thread will be inactive. So, for example, if you cause a dream with 5 seconds, you are guaranteed that your thread will sleep at least 5 seconds. It can be 6 or 8 or 50, depending on what the OS does. (During optimal OS execution, this will be very close to 5.)
Another useful feature of the sleep function is to pass 0. This will force the context switch from your thread.

Additional Information:
http://www.opengroup.org/onlinepubs/000095399/functions/sleep.html

+2
01 Oct '08 at 17:01
source share

I found that "_sleep(milliseconds);" (without quotes) works well for Win32 if you included the chrono library

eg:

 #include <chrono> using namespace std; main { cout << "text" << endl; _sleep(10000); // pauses for 10 seconds } 

Make sure you turn on the underline before bedtime.

+1
Sep 12 '14 at 12:07 on
source share

You can try this piece of code:

 #include<chrono> #include<thread> int main(){ std::this_thread::sleep_for(std::chrono::nanoseconds(10)); std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1)); } 
+1
Sep 12 '14 at 12:25
source share

to delay output to cpp for a fixed time, you can use the Sleep () function by including the windows.h header file. The syntax of the Sleep () function is Sleep (time_in_ms)

 cout<<"Apple\n"; Sleep(3000); cout<<"Mango"; 

OUTPUT. Apple will print above the code and wait 3 seconds before printing the mango.

+1
Apr 18 '17 at 17:09 on
source share

Syntax:

void sleep (unsigned seconds);

sleep () pauses execution for an interval (in seconds). When you call to sleep, the current program is suspended from execution for the seconds specified by the second of the argument. The interval is accurate only to the nearest hundredth of a second or the accuracy of the operating system clock, whichever is less accurate.

0
01 Oct '08 at 16:48
source share

Many others have provided good sleep information. I agree with Wedge that sleep is rarely the most appropriate solution.

If you sleep when you are waiting for something, then it is best for you to wait for this thing / event. Look at the state variables for this.

I donโ€™t know which OS you are trying to do this, but for streaming and synchronization you can look at the Boost Threading libraries ( Boost Condition Varriable ).

Moving now to the other extreme, if you are trying to wait for exceptionally short periods, then there are several hacking style options. If you are working on some kind of built-in platform where "sleep" is not implemented, you can try a simple loop (for / while, etc.) with an empty body (be careful that the compiler does not optimize it). Of course, the wait time depends on the specific equipment in this case. For really short "expectations" you can try the "nop" build. I doubt very much that this is what you need, but not knowing why you need to wait for it to be more specific.

0
02 Oct '08 at 4:43
source share

On Windows, you can enable the window library and use "Sleep (0)"; for sleep. It takes a value representing milliseconds.

0
Oct 02 '08 at 4:52
source share



All Articles