Yes - older POSIX standards are defined by usleep() , so this is available on Linux:
int usleep(useconds_t usec);
DESCRIPTION
The usleep () function pauses the execution thread for (at least) microseconds. Sleep can be slightly lengthened by any system activity or the time spent processing a call, or the granularity of system timers.
usleep() takes microseconds , so you have to multiply your input by 1000 to sleep in milliseconds.
usleep() has since been deprecated and subsequently removed from POSIX; nanosleep() preferred for new code:
#include <time.h> int nanosleep(const struct timespec *req, struct timespec *rem);
DESCRIPTION
nanosleep() pauses the execution of the calling thread until at least the time specified in *req expires, or until the signal that starts the handler call to the calling thread or terminates the process.
The timepec structure is used to specify time intervals accurate to the nanosecond. It is defined as follows:
struct timespec { time_t tv_sec; long tv_nsec; };
An example of the msleep() function, implemented using nanosleep() , which continues to sleep mode if it is interrupted by a signal:
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; }
caf Jul 21 '09 at 3:52 2009-07-21 03:52
source share