Is it possible with macros make cross platform Sleep code? For example #ifdef LINUX #include sleepfunction.h #endif #ifdef WINDOWS #include. Sleep function in C. Ask Question Asked 10 years, 5 months ago. Prior to C11, C had no thread concept and no sleep capability, so your solution was necessarily platform dependent. Here's a snippet that defines a sleep function for Windows or Unix. Delay in Dev C. 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. On Linux, sleep is implemented via nanosleep(2). See the nanosleep(2) man page for a discussion of the clock used. Portability notes On some systems, sleep may be implemented using alarm(2) and SIGALRM (POSIX.1 permits this); mixing calls to alarm(2) and sleep is a bad idea. The Sleep call does not busy-loop. It suspends the current thread to allow other threads to run for the delay duration. This is the description for the dwMilliseconds parameter to Sleep. 'Specifies the time, in milliseconds, for which to suspend execution.
Sleep function in Windows, using C. Ask Question Asked 9 years, 8 months ago. I was searching for some sleep function in windows C, except Sleep.
Previous: Setting an Alarm, Up: Date and Time [Contents][Index]
21.7 Sleeping
The function
sleep
gives a simple way to make the program waitfor a short interval. If your program doesn’t use signals (except toterminate), then you can expect sleep
to wait reliably throughoutthe specified interval. Otherwise, sleep
can return sooner if asignal arrives; if you want to wait for a given interval regardless ofsignals, use select
(see Waiting for I/O) and don’t specifyany descriptors to wait for.Preliminary:| MT-Unsafe sig:SIGCHLD/linux| AS-Unsafe | AC-Unsafe | See POSIX Safety Concepts.
The
sleep
function waits for seconds seconds or until a signalis delivered, whichever happens first.If
sleep
returns because the requested interval is over,it returns a value of zero. If it returns because of delivery of asignal, its return value is the remaining time in the sleep interval.The
sleep
function is declared in unistd.h.Resist the temptation to implement a sleep for a fixed amount of time byusing the return value of
sleep
, when nonzero, to callsleep
again. This will work with a certain amount of accuracy aslong as signals arrive infrequently. But each signal can cause theeventual wakeup time to be off by an additional second or so. Suppose afew signals happen to arrive in rapid succession by bad luck—there isno limit on how much this could shorten or lengthen the wait.Instead, compute the calendar time at which the program should stopwaiting, and keep trying to wait until that calendar time. This won’tbe off by more than a second. With just a little more work, you can use
select
and make the waiting period quite accurate. (Of course,heavy system load can cause additional unavoidable delays—unless themachine is dedicated to one application, there is no way you can avoidthis.)On some systems,
sleep
can do strange things if your program usesSIGALRM
explicitly. Even if SIGALRM
signals are beingignored or blocked when sleep
is called, sleep
mightreturn prematurely on delivery of a SIGALRM
signal. If you haveestablished a handler for SIGALRM
signals and a SIGALRM
signal is delivered while the process is sleeping, the action takenmight be just to cause sleep
to return instead of invoking yourhandler. And, if sleep
is interrupted by delivery of a signalwhose handler requests an alarm or alters the handling of SIGALRM
,this handler and sleep
will interfere.On GNU systems, it is safe to use
sleep
and SIGALRM
inthe same program, because sleep
does not work by means ofSIGALRM
.Preliminary:| MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
If resolution to seconds is not enough the
nanosleep
function canbe used. As the name suggests the sleep interval can be specified innanoseconds. The actual elapsed time of the sleep interval might belonger since the system rounds the elapsed time you request up to thenext integer multiple of the actual resolution the system can deliver.*
requested_time
is the elapsed time of the interval you want tosleep.The function returns as *
remaining
the elapsed time left in theinterval for which you requested to sleep. If the interval completedwithout getting interrupted by a signal, this is zero.struct timespec
is described in Time Types.If the function returns because the interval is over the return value iszero. If the function returns -1 the global variable
errno
is set to the following values:EINTR
The call was interrupted because a signal was delivered to the thread.If the remaining parameter is not the null pointer the structurepointed to by remaining is updated to contain the remainingelapsed time.
EINVAL
The nanosecond value in the requested_time parameter contains anillegal value. Either the value is negative or greater than or equal to1000 million.
Dev C Sleep System
This function is a cancellation point in multi-threaded programs. Thisis a problem if the thread allocates some resources (like memory, filedescriptors, semaphores or whatever) at the time
nanosleep
iscalled. If the thread gets canceled these resources stay allocateduntil the program ends. To avoid this calls to nanosleep
shouldbe protected using cancellation handlers.Dev C++ Online
The
nanosleep
function is declared in time.h.Dev C++ Sqrt
Previous: Setting an Alarm, Up: Date and Time [Contents][Index]