#include #include #include #include #include #define CHECKER(exp) do {int ret=exp;if(ret){fprintf(stderr,"[%d] "#exp" ret %d\n",__LINE__,ret);}}while(0) static void clock_id_test(clockid_t clock_id){ pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond; pthread_condattr_t condattr; struct timespec thistime; char buf[64]; memset (buf, 0, sizeof(buf)); CHECKER(pthread_condattr_init(&condattr)); CHECKER(pthread_condattr_setclock(&condattr, clock_id)); CHECKER(pthread_cond_init(&cond, &condattr)); CHECKER(pthread_mutex_lock (&mtx)); CHECKER(clock_gettime(clock_id, &thistime)); ctime_r(&thistime.tv_sec, buf); fprintf (stderr, "start %s", buf); thistime.tv_sec += 5; ctime_r(&thistime.tv_sec, buf); fprintf (stderr, "will wakeup at %s", buf); CHECKER(pthread_cond_timedwait(&cond, &mtx, &thistime)); CHECKER(pthread_mutex_unlock(&mtx)); CHECKER(clock_gettime(clock_id, &thistime)); ctime_r(&thistime.tv_sec, buf); fprintf (stderr, "end %s", buf); } int main (int argc, char *argv[]) { fprintf(stderr,"ETIMEDOUT is %d\n", ETIMEDOUT); clock_id_test(CLOCK_REALTIME); clock_id_test(CLOCK_MONOTONIC); return 0; }