sem_wait()

Updated: April 19, 2023

Wait on a named or unnamed semaphore

Synopsis:

#include <semaphore.h>

int sem_wait( sem_t * sem );

Arguments:

sem
A pointer to the sem_t object for the semaphore that you want to wait on.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The sem_wait() function decrements the semaphore referred to by the sem argument. If the semaphore value isn't greater than zero, then the calling thread blocks until it can decrement the counter (i.e., another thread posts the semaphore), or the call is interrupted by a signal.

Some thread should eventually call sem_post() to increment the semaphore.

Returns:

0
The semaphore was successfully decremented.
-1
The state of the semaphore is unchanged (errno is set).

Errors:

EINVAL
Invalid semaphore descriptor sem.
EINTR
A signal interrupted this function.

Classification:

POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes