Name
      
      pthread_cond_timedwait 
      
      
      - wait on a condition variable for a specific amount of time
      
      
      
      
         
         Library
      
      
      
      
      libc_r.lib
      
      
      libpthread.lib
      
      
      libthr.lib
      
      
      
      
         
         Synopsis
      
      
      
      
      
      
      
         
            |  | int
               
               
               pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); 
 | 
      
      
         
         Return values
      
      
      If successful, the
      
      
       pthread_cond_timedwait
      function will return zero.
      Otherwise an error number will be returned to
      indicate the error.
      
      
      
      
         
         Detailed description
      
      
      The
      
      
       pthread_cond_timedwait
      function atomically blocks the current thread waiting on the condition
      variable specified by
      
      
       cond,
      and unblocks the mutex specified by
      
      
       mutex.
      The waiting thread unblocks only after another thread calls
      
      
      pthread_cond_signal,
      or
      
      
      pthread_cond_broadcast
      with the same condition variable, or if the system time reaches the
      time specified in
      
      
       abstime,
      and the current thread reacquires the lock on
      
      
       mutex.
      
      
      
      
         
         Examples
      
      
      
      
      struct testdata
{
 pthread_mutex_t mutex;
 pthread_cond_t  cond;
};
static struct testdata td;
 int rc;
 struct timespec timeout;
 struct timeval  curtime;
/* mutex and conditional variable must be initialized */
 if (pthread_mutex_lock(&td.mutex) != 0) {
  fprintf(stderr,"Thread: Fail to acquire mutex\n");
  return -1;
 }
 if (gettimeofday(&curtime, NULL) !=0 ) {
  fprintf(stderr,"Fail to get current time\n");
  return -1;
 }
 timeout.tv_sec = curtime.tv_sec;
 timeout.tv_nsec = curtime.tv_usec * 1000;
 timeout.tv_sec += TIMEOUT;
 fprintf(stderr,"Thread is waiting for the cond\n");
 rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout);
 if(rc != 0) {
  if (rc == ETIMEDOUT) {
   fprintf(stderr,"Thread stops waiting when time is out\n");
   return -1;
  }
  else {
   fprintf(stderr,"pthread_cond_timedwait return %d\n", rc);
   return -1;
  }
 }
 fprintf(stderr,"Thread wakened up\n");
 pthread_mutex_unlock(&td.mutex);
         
      
         
         Errors
      
      
      The
      
      
       pthread_cond_timedwait
      function will fail if:
      
      
      
      
      
      
         
            | [EINVAL] | 
         
            |  | The value specified by
               
               
                cond,
               
               
                mutex
               or
               
               
                abstime
               is invalid. | 
         
            | [ETIMEDOUT] | 
         
            |  | The system time has reached or exceeded the time specified in
               
               
                abstime. | 
         
            | [ENOMEM] | 
         
            |  | If this function was not able to acquire a required resource. | 
         |
      
      
      
      
         
         See also
      
      
      
      
      pthread_cond_broadcast,
      
      
      pthread_cond_destroy,
      
      
      pthread_cond_init,
      
      
      pthread_cond_signal,
      
      
      pthread_cond_wait
      
      
      Feedback
For additional information or queries on this page send feedback	
      
      
         
         
         
            
               | © 2005-2007 Nokia  | 
 |