[Previous]
[Contents]
[Next]

sem_trywait()

wait on a semaphore, but don't block

Synopsis:

#include <semaphore.h>
int sem_trywait( sem_t *sem );

Description:

The sem_trywait() function decrements the semaphore if the semaphore's value is greater than zero, otherwise the function simply returns.

Returns:

If the semaphore was successfully decremented, sem_trywait() returns zero. Otherwise, the state of the semaphore is unchanged and -1 is returned, with errno set to indicate the error.

Errors:

EAGAIN
The semaphore was already locked, so it couldn't be immediately locked by the sem_trywait() function.
EDEADLK
A deadlock condition was detected.
EINVAL
The sem argument doesn't refer to a valid semaphore.
EINTR
A signal interrupted this function.
ENOSYS
The function sem_trywait() isn't supported.

Examples:

/usr/demo/src/semaphores

Classification:

POSIX 1003.1b-1993

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, sem_destroy(), sem_init(), sem_post(), sem_wait()


[Previous]
[Contents]
[Next]