How to Add a Timed Delay to a C++ Program

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

In Win32:

#include<windows.h>
Sleep(milliseconds);

In Unix:

#include<unistd.h>
unsigned int microsecond = 1000000;
usleep(3 * microsecond);//sleeps for 3 second

sleep() only takes a number of seconds which is often too long.

implement time delay in c

In standard C (C99), you can use time() to do this, something like:

#include <time.h>
:
void waitFor (unsigned int secs) {
unsigned int retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}

By the way, this assumes time() returns a 1-second resolution value. I don't think that's mandated by the standard so you may have to adjust for it.


In order to clarify, this is the only way I'm aware of to do this with ISO C99 (and the question is tagged with nothing more than "C" which usually means portable solutions are desirable although, of course, vendor-specific solutions may still be given).

By all means, if you're on a platform that provides a more efficient way, use it. As several comments have indicated, there may be specific problems with a tight loop like this, with regard to CPU usage and battery life.

Any decent time-slicing OS would be able to drop the dynamic priority of a task that continuously uses its full time slice but the battery power may be more problematic.

However C specifies nothing about the OS details in a hosted environment, and this answer is for ISO C and ISO C alone (so no use of sleep, select, Win32 API calls or anything like that).

And keep in mind that POSIX sleep can be interrupted by signals. If you are going to go down that path, you need to do something like:

int finishing = 0; // set finishing in signal handler 
// if you want to really stop.

void sleepWrapper (unsigned int secs) {
unsigned int left = secs;
while ((left > 0) && (!finishing)) // Don't continue if signal has
left = sleep (left); // indicated exit needed.
}

How to add a delay to code in C++.

in c++ 11 you can use this thread and crono to do it:

#include <chrono>
#include <thread>
...
using namespace std::chrono_literals;
...
std::this_thread::sleep_for(2s);

Is it possible to wait a few seconds before printing a new line in C?

Well the sleep() function does it, there are several ways to use it;

On linux:

#include <stdio.h>
#include <unistd.h> // notice this! you need it!

int main(){
printf("Hello,");
sleep(5); // format is sleep(x); where x is # of seconds.
printf("World");
return 0;
}

And on windows you can use either dos.h or windows.h like this:

#include <stdio.h>
#include <windows.h> // notice this! you need it! (windows)

int main(){
printf("Hello,");
Sleep(5); // format is Sleep(x); where x is # of milliseconds.
printf("World");
return 0;
}

or you can use dos.h for linux style sleep like so:

#include <stdio.h>
#include <dos.h> // notice this! you need it! (windows)

int main(){
printf("Hello,");
sleep(5); // format is sleep(x); where x is # of seconds.
printf("World");
return 0;
}

And that is how you sleep in C on both windows and linux! For windows both methods should work. Just change the argument for # of seconds to what you need, and insert wherever you need a pause, like after the printf as I did. Also, Note: when using windows.h, please remember the capital S in sleep, and also thats its milliseconds! (Thanks to Chris for pointing that out)

How to delay output in C++?

This is platform-dependent.

On Windows, you may use Sleep() function:

Sleep(3000);

The parameter is the amount of time in milliseconds. You need to include windows.h in order to use this function.

On POSIX-compatible, you may use sleep():

sleep(3); // parameter is in seconds

If you need better precision, you may use usleep():

usleep(3000000); // parameter is in microseconds

For both functions, you need to include unistd.h

Delay function in C not delaying

Use the nop () function to insert a number of NO-OP instructions into your C code. Figure out the amount of time it takes for a single NOP on your target and use as many as necessary. Source: keil.com/support/docs/606.htm

For this "sample" y try with Delay(1000) you can make changes to this value;

#include <LPC21xx.H>
#include <intrins.h>

#pragma O0
void Delay(volatile uint32_t cnt) {
while(cnt--)
_nop_();
}

void DelayWithoutNop(volatile uint32_t cnt) {
while(cnt--);
}

int main(){
//set pin 16 P1 as out
IO1DIR = 0x10000;
//set pin 16 P1 on 1
IO1SET = 0x10000;
Delay(1000);
DelayWithotNop(3000);
//set pin 16 port P1 on 0
IO1CLR = 0x10000;

}


Related Topics



Leave a reply



Submit