Sleep Function in C++

What is the proper #include for the function 'sleep()'?

The sleep man page says it is declared in <unistd.h>.

Synopsis:

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

Sleep function in Windows, using C

Use:

#include <windows.h>

Sleep(sometime_in_millisecs); // Note uppercase S

And here's a small example that compiles with MinGW and does what it says on the tin:

#include <windows.h>
#include <stdio.h>

int main() {
printf( "starting to sleep...\n" );
Sleep(3000); // Sleep three seconds
printf("sleep ended\n");
}

C sleep function not working

There's nothing wrong with the code, but note that in many cases the output of printf is buffered, meaning that the output appears on the console only if you explicitly call fflush(stdout), you print a newline, or the buffer becomes full. Since you don't print a newline until the very end, you will see nothing in the for loop for 40 seconds (because the stuff that printf printed is still in the buffer). Then, when the execution hits printf("\n"), everything will be printed at once as the buffer is flushed.

So, the bottom line is: either call fflush(stdout) before you call sleep to ensure that nothing stays in the output buffer, or wait for 40 seconds and you will get the output in a single batch in the end.

Is there an alternative sleep function in C to milliseconds?

Yes - older POSIX standards defined usleep(), so this is available on Linux:

int usleep(useconds_t usec);

DESCRIPTION

The usleep() function suspends execution of the calling thread for
(at least) usec microseconds. The sleep may be lengthened slightly by
any system activity or by the time spent processing the call or by the
granularity of system timers.

usleep() takes microseconds, so you will have to multiply the input by 1000 in order to sleep in milliseconds.


usleep() has since been deprecated and subsequently removed from POSIX; for new code, nanosleep() is preferred:

#include <time.h>

int nanosleep(const struct timespec *req, struct timespec *rem);

DESCRIPTION

nanosleep() suspends the execution of the calling thread until either at least the time specified in *req has elapsed, or the
delivery of a signal that triggers the invocation of a handler in the
calling thread or that terminates the process.

The structure timespec is used to specify intervals of time with nanosecond precision. It is defined as follows:

struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

An example msleep() function implemented using nanosleep(), continuing the sleep if it is interrupted by a signal:

#include <time.h>
#include <errno.h>

/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(long msec)
{
struct timespec ts;
int res;

if (msec < 0)
{
errno = EINVAL;
return -1;
}

ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;

do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);

return res;
}

Sleep() function in C not working on hp non-stop

The result calling sleep() from guardian environment is undefined. That might be leading to ABEND that you mentioned. If you want to wait for some time in guardian hp-nonstop environment, you should call DELAY(). It takes centi-seconds as arguments. So if you want to add delay of 5 seconds, you should call it as DELAY (500). You also need to include the header #include<cextdecs(DELAY)>

What should I use to stop the sleep() function in C if a condition changes in that period of time (doesn't hold anymore)?

I would not call either MS Sleep() or gcc sleep() but go for a non-blocking function, as you have with MS kbhit().

For a 1 second timer an example use is

clock_t target = clock() + CLOCKS_PER_SEC;
while(!kbhit() && clock() < target) {
// some code if needed
}

The loop will end when a key is pressed, or after 1 second. You can check kbhit() again to find out which.

Is there an alternative for sleep() in C?

Alternatives depend in what you are trying to do and what OS you are on.

If you just want to waste time, then these might help:

On most unix-type systems you'll find a 'usleep' function, which is more or less like sleep with greater resolution. Be careful with that one because it usually can not sleep for just one microsecond.

On some unix-type systems, the select system call can be used with all file descriptor sets zero in order to get a fairly accurate sub-second wait.

On windows systems, you have Sleep, which is pretty much the same, but taking a number of milliseconds.

In a multi-tasking operating system, a sleep function can sometimes be given 0 as a parameter. This generally causes the function to give up it's timeslice, but be re-scheduled immediately if no other task is ready to run.

Sleep for milliseconds

Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for usleep, which accepts microseconds:

#include <unistd.h>

unsigned int microseconds;
...
usleep(microseconds);


Related Topics



Leave a reply



Submit