usleep()--Suspend Processing for Interval of Time


  Syntax
 #include <unistd.h>

 unsigned int usleep( useconds_t useconds );  

  Service Program Name: QP0SSRV1

  Default Public Authority: *USE

  Threadsafe: Yes

The usleep() function suspends a thread for the number of microseconds specified by the of useconds parameter. (Because of processor delays, the thread can be suspended slightly longer than this specified time.)

The usleep() function uses the process's real-time interval timer to indicate when the thread should be resumed.

There is one real-time interval timer for each process. The usleep() function will not interfere with a previous setting of this timer.


Authorities and Locks

None.


Parameters

useconds
(Input) The number of microseconds for which the thread is to be suspended.

Return Value

0 The thread slept for the full time specified.
-1 sleep() was not successful. The errno variable is set to indicate the error.


Error Conditions

If usleep() is not successful, errno usually indicates the following error. Under some conditions, errno could indicate an error other than that listed here.

[EINVAL]
The value specified for the argument is not correct.

A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.

An argument value is not valid, out of range, or NULL.


Usage Notes

The usleep() function is included for its historical usage. The setitimer() function is preferred over this function.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example uses the usleep() function to suspend processing for a specified time:

#include <unistd.h>
#include <stdio.h>
#include <time.h>

void timestamp( char *str ) {

    time_t t;

    time( &t );
    printf( "%s the time is %s\nquot;, str, ctime(&t) );
}

int main( int argc, char *argv[] ) {

    int result = 0;

    timestamp( quot;before usleep()quot; );
    result = usleep( 999999 );
    timestamp( quot;after usleep()quot; );

    printf( quot;usleep() returned %d\nquot;, result );

    return( result );
}

Output:

    before usleep() the time is Sun Jun 15 17:25:17 1995
    after usleep() the time is Sun Jun 15 17:25:18 1995
    usleep() returned 0


API introduced: V4R2
Top | UNIX-Type APIs | APIs by category