Name

sem_timedwait
- decrement (lock) a semaphore

Library

libpthread.lib

Synopsis

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

Return values

Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The sem_timedwait function decrements (locks) the semaphore pointed to by sem, but blocks if the value of sem is zero, until the value is non-zero or timeout occurs.


Examples

sem_t mysemp;
struct timespec ts;
int val, sts;
#define TIMEOUT 3
if ( sem_init (&mysemp, 0, 1) == -1 ) {
      perror( "sem_init failed");
      return -1;
}
       
struct timeval now;
gettimeofday(&now,NULL);
ts.tv_sec=now.tv_sec + TIMEOUT;
ts.tv_nsec=0;
/* Lock Semaphore */
if (sem_timedwait(&mysemp, &ts) == 0)
{  
    switch(errno)
    {
     case 0:
        printf ("Locked successfully\n");
        break;
     case ETIMEDOUT:
        printf ("could not lock, try later.....\n");
        break;
    }
}
else
    printf ("Sem_timedwait failed \n")
fprintf(stderr,"Thread wakened up\n");


Errors

The sem_timedwait function will fail if:
[EINVAL]
  The sem argument points to an invalid or uninitialized semaphore.
[ETIMEDOUT]
  The semaphore could not be locked before the specified timeout value.


See also

sem_getvalue, sem_post


Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top