pthread_cond_wait() — Wait on a condition variable

Standards

Standards / Extensions C or C++ Dependencies

POSIX.4a
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
SUSV3:
#define _UNIX03_THREADS 
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t * __restrict__cond, 
                      pthread_mutex_t * __restrict__mutex);

General description

Blocks on a condition variable. It must be called with mutex locked by the calling thread, or undefined behavior will result. A mutex is locked using pthread_mutex_lock().

cond is a condition variable that is shared by threads. To change it, a thread must hold the mutex associated with the condition variable. The pthread_cond_wait() function releases this mutex before suspending the thread and obtains it again before returning.

The pthread_cond_wait() function waits until a pthread_cond_broadcast() or a pthread_cond_signal() is received. For more information on these functions, refer to pthread_cond_broadcast() — Broadcast a condition and to pthread_cond_signal() — Signal a condition.

Returned value

If successful, pthread_cond_wait() returns 0.

If unsuccessful, pthread_cond_wait() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
Different mutexes were specified for concurrent operations on the same condition variable.
EPERM
The mutex was not owned by the current thread at the time of the call.

Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_cond_wait() returns an error number to indicate the error.

Usage notes

If the condition variable is shared (PTHREAD_PROCESS_SHARED), the mutex must also be shared, with the _OPEN_SYS_MUTEX_EXT feature defined when the mutex was created and initialized.

If the condition variable is private (PTHREAD_PROCESS_PRIVATE), the mutex must also be private.

If the condition variable is shared, all calls to pthread_cond_wait() or pthread_cont_timedwait() for a given condition variable must use the same mutex for the life of the process, or until both the condition variable and mutex are destroyed (using pthread_cond-destroy() and pthread_mutex_destroy()).

Example

CELEBP22
⁄* CELEBP22 *⁄                                   
#define _OPEN_THREADS                                                           
#include <pthread.h>                                                            
#include <stdio.h>                                                              
#include <time.h>                                                               
#include <unistd.h>                                                             
                                                                                
pthread_cond_t cond;                                                            
pthread_mutex_t mutex;                                                          
                                                                                
int footprint = 0;                                                              
                                                                                
void *thread(void *arg) {                                                       
  time_t T;                                                                     
                                                                                
  if (pthread_mutex_lock(&mutex) != 0) {                                        
    perror("pthread_mutex_lock() error");                                       
    exit(6);                                                                    
  }                                                                             
  time(&T);                                                                     
  printf("starting wait at %s", ctime(&T));                                     
  footprint++;                                                                  
                                                                                
  if (pthread_cond_wait(&cond, &mutex) != 0) {                                  
    perror("pthread_cond_timedwait() error");                                   
    exit(7);                                                                    
  }                                                                             
  time(&T);                                                                     
  printf("wait over at %s", ctime(&T));                                         
}                                                                               
                                                                                
main() {                                                                        
  pthread_t thid;                                                               
  time_t T;                                                                     
  struct timespec t;                                                            
                                                                                
  if (pthread_mutex_init(&mutex, NULL) != 0) {                                  
    perror("pthread_mutex_init() error");                                       
    exit(1);                                                                    
  }                                                                             
                                                                                
  if (pthread_cond_init(&cond, NULL) != 0) {                                    
    perror("pthread_cond_init() error");                                        
    exit(2);                                                                    
  }                                                                             
                                                                                
  if (pthread_create(&thid, NULL, thread, NULL) != 0) {                         
    perror("pthread_create() error");                                           
    exit(3);                                                                    
  }                                                                             
                                                                                
  while (footprint == 0)                                                        
    sleep(1);                                                                   
                                                                                
  puts("IPT is about ready to release the thread");                             
  sleep(2);                                                                     
                                                                                
  if (pthread_cond_signal(&cond) != 0) {                                        
    perror("pthread_cond_signal() error");                                      
    exit(4);                                                                    
  }                                                                             
                                                                                
  if (pthread_join(thid, NULL) != 0) {                                          
    perror("pthread_join() error");                                             
    exit(5);                                                                    
  }                                                                             
}                                                                               
Output:
starting wait at Fri Jun 16 10:54:06 2001
IPT is about ready to release the thread
wait over at Fri Jun 16 10:54:09 2001

Related information