1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <sys/time.h> #include <errno.h> #include <assert.h> static pthread_mutex_t testMutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t testCond = PTHREAD_COND_INITIALIZER; static void mywait(int time_secs) { struct timespec now; int rt; clock_gettime(CLOCK_REALTIME, &now); now.tv_sec += time_secs; assert( pthread_mutex_lock(&testMutex) == 0 ); rt = pthread_cond_timedwait(&testCond, &testMutex, &now); assert( rt == ETIMEDOUT || rt == 0 ); assert( pthread_mutex_unlock(&testMutex) == 0 ); printf("Done.\n"); } static void * fun(void *arg) { printf("Thread wait ..\n"); mywait(1); mywait(1); return NULL; } int main(void) { pthread_t thread; void *ret; assert( pthread_create(&thread, NULL, fun, NULL) == 0 ); usleep(50000); assert( pthread_cond_signal(&testCond) == 0 ); printf("Mainthread: join\n"); assert( pthread_join(thread, &ret) == 0 ); return 0; }