sem_timedwait

Locks a semaphore, blocking for the specified time period if necessary.

Library:LibC
Classification:POSIX
Service:Synchronization

Syntax

  #include <semaphore.h>
   
  int  sem_timedwait  (
     sem_t                   *sem,
     const struct timespec   *abstime);
  

Parameters

sem

(IN) Points to the semaphore to acquire.

abstime

(IN) Points to a timespec structure which specifies the maximum amount of time you are willing to wait. See timespec_t, which is also called timespec and timestrc_t.

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.

60

ETIMEDOUT

The semaphore could not be locked before the specified timeout expired.

Remarks

If the semaphore is immediately available, the sema_timedwait function locks the semaphore and returns. If the semaphore is not immediately available, the calling thread blocks on the semaphore and waits the specified time. If the semaphore becomes available during this time, the sema_timedwait function locks the semaphore and returns. If it doesn't become available during this time, the function sets errno to ETIMEDOUT.

The time interval specified in the abstime parameter specifies the absolute time passed which you do not want to wait, such as 10 a.m. today. To enter that value in the abstime parameter, you combine the current UTC time plus the time required to get to the absolute time.

See Also