usleep() — Suspend execution for an interval

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>

int usleep(useconds_t useconds);

General description

The usleep() function suspends thread execution for the number of microseconds specified by the useconds argument. Because of other activity, or because of the time spent processing the call, the actual suspension time may be longer than the amount of time specified.

The useconds argument must be less than 1,000,000. If the value of useconds is 0, then the call has no effect.

The usleep() function will not interfere with a previous setting of the real-time interval timer. If the thread has set this timer before calling usleep(), and if the time specified by useconds equals or exceeds the interval timer's prior setting, then the thread will be awakened when the previously set timer interval expires.

Note: The ualarm() and usleep() functions have been moved to obsolescence in Single UNIX Specification, Version 3 and may be withdrawn in a future version. The setitimer(), timer_create(), timer_delete(), timer_getoverrun(), timer_gettime(), or timer_settime() functions are preferred for portability.

Returned value

If successful, usleep() returns 0.

If unsuccessful, usleep() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
The useconds argument was greater than or equal to 1,000,000.

Related information