sem_trywait

Makes a single attempt to acquire the resource protected by a semaphore.

Library:LibC
Classification:POSIX
Service:Synchronization

Syntax

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

Parameters

sem

(IN) Points to the semaphore to acquire.

Return Values

If successful, returns 0. Otherwise, returns -1 and sets errno to one of the following values.

Decimal

Constant

Description

9

EINVAL

The sem parameter is invalid.

62

EBUSY

The semaphore is not currently available.

Remarks

If the semaphore count is greater than zero, the sem_trywait function decrements the semaphore value and returns 0 to indicate a successful acquisition of the resource.

If the semaphore count is zero or less than zero, the semaphore cannot be immediately acquired. Because sem_trywait cannot block and wait for the resource to become available, the function returns EBUSY to indicate failure and does not modify the semaphore value.

See Also