1995e36deffc5fcdff74fbc41b2bbb342b0350484bart/* 2995e36deffc5fcdff74fbc41b2bbb342b0350484bart * Invoke pthread_cond_destroy() on a condition variable that is being waited 3995e36deffc5fcdff74fbc41b2bbb342b0350484bart * upon. 4995e36deffc5fcdff74fbc41b2bbb342b0350484bart */ 5995e36deffc5fcdff74fbc41b2bbb342b0350484bart 6995e36deffc5fcdff74fbc41b2bbb342b0350484bart#include <assert.h> 7995e36deffc5fcdff74fbc41b2bbb342b0350484bart#include <errno.h> 8995e36deffc5fcdff74fbc41b2bbb342b0350484bart#include <stdio.h> // printf() 9995e36deffc5fcdff74fbc41b2bbb342b0350484bart#include <pthread.h> 10995e36deffc5fcdff74fbc41b2bbb342b0350484bart 11995e36deffc5fcdff74fbc41b2bbb342b0350484bartstatic pthread_mutex_t s_mutex; 12995e36deffc5fcdff74fbc41b2bbb342b0350484bartstatic pthread_cond_t s_cond; 13995e36deffc5fcdff74fbc41b2bbb342b0350484bartstatic int s_i; 14995e36deffc5fcdff74fbc41b2bbb342b0350484bart 15995e36deffc5fcdff74fbc41b2bbb342b0350484bartstatic const char* err_to_str(int errnum) 16995e36deffc5fcdff74fbc41b2bbb342b0350484bart{ 17995e36deffc5fcdff74fbc41b2bbb342b0350484bart switch (errnum) { 18995e36deffc5fcdff74fbc41b2bbb342b0350484bart case 0: return "success"; 19995e36deffc5fcdff74fbc41b2bbb342b0350484bart case EBUSY: return "EBUSY"; 20995e36deffc5fcdff74fbc41b2bbb342b0350484bart case EINVAL: return "EINVAL"; 21995e36deffc5fcdff74fbc41b2bbb342b0350484bart default: return "?"; 22995e36deffc5fcdff74fbc41b2bbb342b0350484bart } 23995e36deffc5fcdff74fbc41b2bbb342b0350484bart} 24995e36deffc5fcdff74fbc41b2bbb342b0350484bart 25995e36deffc5fcdff74fbc41b2bbb342b0350484bartstatic void* thread_func(void* thread_arg) 26995e36deffc5fcdff74fbc41b2bbb342b0350484bart{ 27995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_lock(&s_mutex); 28995e36deffc5fcdff74fbc41b2bbb342b0350484bart s_i = 1; 29995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_cond_signal(&s_cond); 30995e36deffc5fcdff74fbc41b2bbb342b0350484bart while (s_i == 1) 31995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_cond_wait(&s_cond, &s_mutex); 32995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_unlock(&s_mutex); 33995e36deffc5fcdff74fbc41b2bbb342b0350484bart 34995e36deffc5fcdff74fbc41b2bbb342b0350484bart return 0; 35995e36deffc5fcdff74fbc41b2bbb342b0350484bart} 36995e36deffc5fcdff74fbc41b2bbb342b0350484bart 37995e36deffc5fcdff74fbc41b2bbb342b0350484bartint main(int argc, char** argv) 38995e36deffc5fcdff74fbc41b2bbb342b0350484bart{ 39995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_t threadid; 40995e36deffc5fcdff74fbc41b2bbb342b0350484bart int ret; 41995e36deffc5fcdff74fbc41b2bbb342b0350484bart 42995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_init(&s_mutex, 0); 43995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_cond_init(&s_cond, 0); 44995e36deffc5fcdff74fbc41b2bbb342b0350484bart 45995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_create(&threadid, 0, thread_func, 0); 46995e36deffc5fcdff74fbc41b2bbb342b0350484bart 47995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_lock(&s_mutex); 48995e36deffc5fcdff74fbc41b2bbb342b0350484bart while (s_i == 0) 49995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_cond_wait(&s_cond, &s_mutex); 50995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_unlock(&s_mutex); 51995e36deffc5fcdff74fbc41b2bbb342b0350484bart 52995e36deffc5fcdff74fbc41b2bbb342b0350484bart ret = pthread_cond_destroy(&s_cond); 53995e36deffc5fcdff74fbc41b2bbb342b0350484bart fprintf(stderr, "First pthread_cond_destroy() call returned %s.\n", 54995e36deffc5fcdff74fbc41b2bbb342b0350484bart err_to_str(ret)); 55995e36deffc5fcdff74fbc41b2bbb342b0350484bart 56995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_lock(&s_mutex); 57995e36deffc5fcdff74fbc41b2bbb342b0350484bart s_i = 2; 58995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_cond_signal(&s_cond); 59995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_unlock(&s_mutex); 60995e36deffc5fcdff74fbc41b2bbb342b0350484bart 61995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_join(threadid, 0); 62995e36deffc5fcdff74fbc41b2bbb342b0350484bart 63995e36deffc5fcdff74fbc41b2bbb342b0350484bart ret = pthread_cond_destroy(&s_cond); 64995e36deffc5fcdff74fbc41b2bbb342b0350484bart fprintf(stderr, "Second pthread_cond_destroy() call returned %s.\n", 65995e36deffc5fcdff74fbc41b2bbb342b0350484bart err_to_str(ret)); 66995e36deffc5fcdff74fbc41b2bbb342b0350484bart pthread_mutex_destroy(&s_mutex); 67995e36deffc5fcdff74fbc41b2bbb342b0350484bart 68995e36deffc5fcdff74fbc41b2bbb342b0350484bart return 0; 69995e36deffc5fcdff74fbc41b2bbb342b0350484bart} 70