Wait / Pause the number of seconds in C

I wrote a small console application, and I want it to pause for a certain number of seconds before the cycle (time) starts again.

I am working on a Windows operating system.

+6
source share
3 answers

On Windows, the function for this is Sleep , which takes the milliseconds you want to sleep. To use Sleep , you need to enable windows.h .

On POSIX systems, the Sleep function (from unistd.h ) does the following:

  unsigned int sleep(unsigned int seconds); DESCRIPTION sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored. 

If the signal is interrupted by the signal, the remaining timeout is returned. If you use signals, a more reliable solution would be:

  unsigned int time_to_sleep = 10; // sleep 10 seconds while(time_to_sleep) time_to_sleep = sleep(time_to_sleep); 

This, of course, assumes that your signal handlers take up only a small amount of time. (Otherwise, this will delay the main program longer than expected)

+10
source

easy:

 while( true ) { // your stuff sleep( 10 ); // sleeping for 10 seconds }; 
+3
source

On UNIX :

 #include <unistd.h> sleep(10000); // 10 seconds 

On Windows :

 #include <windows.h> Sleep(10000); // 10 seconds 

Note the difference between sleep() (UNIX) and sleep() (Windows).

0
source

Source: https://habr.com/ru/post/908212/


All Articles