sem_wait(), sem_trywait()

lock a semaphore 

Realtime Function


SYNOPSIS

#include <semaphore.h>

int sem_wait(sem_t *sem);

int sem_trywait(sem_t *sem);


DESCRIPTION

The sem_wait() function locks the specified semaphore by performing a semaphore lock operation on that semaphore. If the semaphore currently has a value of zero, the calling thread does not return from the call to sem_wait() until sem_wait() either locks the semaphore or a signal interrupts the call.

The sem_trywait() function locks the specified semaphore only if that semaphore is currently not locked; that is, if the semaphore value is currently positive. Otherwise, it does not lock the semaphore.

Upon successful return, the state of the semaphore is locked and remains locked until the sem_post() function is executed and returns successfully.

The delivery of a signal can interrupt the sem_wait() function.


PARAMETERS

sem 

Specifies the semaphore to be locked.


RETURN VALUES

When the sem_wait() and sem_trywait() functions successfully perform the lock operation, they return zero. Otherwise, the state of the semaphore is unchanged, and they return -1, and errno is set to one of the following values:

EAGAIN 

The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation (sem_trywait() only).

EINTR 

A signal interrupted this function.

EINVAL

The sem argument does not refer to a valid semaphore.


EXAMPLES

None.


CONFORMANCE

UNIX 03. Derived from the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995)


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

None.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
sem_post(), semctl(), semget(), semop()

Miscellaneous:
semaphores


PTC MKS Toolkit 10.4 Documentation Build 39.