Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a timed delay to a C++ program?

Tags:

c++

time

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

I wish I had more details on how I am implementing this timed delay, but until I have more information on how to add a timed delay I am not sure on how I should even attempt to implement this.

like image 657
Matt Pascoe Avatar asked Oct 01 '08 16:10

Matt Pascoe


People also ask

Is there a delay function in C?

The delay() function is built upon a C library function called clock(). The clock() function returns a time value in clock ticks, which is based on the processor's speed. The value returned is of the clock_t variable type. You can use subsequent reads of the clock() function to determine elapsed time.

What is time delay in C programming?

Delay in C: delay function is used to suspend execution of a program for a particular time. Declaration: void delay(unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the "dos.

How is delay calculated in embedded C?

The first method is simply using Loop program function in which Delay() function is made or by providing for(); delay loop in Embedded C programming. You can define your own value of delay and how long you want to display. For example- for(i=0;i<"any decimal value";i++); this is the delay for loop used in embedded C.

How do you delay a loop?

The simplest option is to just use the __delay_ms() function alone; you just enter the milliseconds you want it to delay and that's it.


1 Answers

An updated answer for C++11:

Use the sleep_for and sleep_until functions:

#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 there's no longer a need to continually add new functions for better resolution: sleep, usleep, nanosleep, etc. sleep_for and sleep_until are template functions that can accept values of any resolution via chrono types; hours, seconds, femtoseconds, etc.

In C++14 you can further simplify the code with the 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); } 

Note that the actual duration of a sleep depends on the implementation: You can ask to sleep for 10 nanoseconds, but an implementation might end up sleeping for a millisecond instead, if that's the shortest it can do.

like image 156
bames53 Avatar answered Sep 22 '22 07:09

bames53