1 /* $Id: condcancel1.c,v 1.2 2003/11/21 19:25:50 nathanw Exp $ */
8 void *threadfunc(void *);
11 pthread_mutex_t mutex
;
16 main(int argc
, char *argv
[])
21 printf("Test of CV state after cancelling a wait\n");
23 ret
= pthread_mutex_init(&mutex
, NULL
);
24 if (ret
) errx(1, "pthread_mutex_init: %s", strerror(ret
));
26 ret
= pthread_cond_init(&cond
, NULL
);
27 if (ret
) errx(1, "pthread_cond_init: %s", strerror(ret
));
29 ret
= pthread_mutex_lock(&mutex
);
30 if (ret
) errx(1, "pthread_mutex_lock: %s", strerror(ret
));
32 ret
= pthread_create(&thread
, NULL
, threadfunc
, NULL
);
33 if (ret
) errx(1, "pthread_create: %s", strerror(ret
));
36 ret
= pthread_cond_wait(&cond
, &mutex
);
37 if (ret
) errx(1, "pthread_cond_wait: %s", strerror(ret
));
40 ret
= pthread_mutex_unlock(&mutex
);
41 if (ret
) errx(1, "pthread_mutex_unlock: %s", strerror(ret
));
43 ret
= pthread_cancel(thread
);
44 if (ret
) errx(1, "pthread_cancel: %s", strerror(ret
));
46 ret
= pthread_join(thread
, NULL
);
47 if (ret
) errx(1, "pthread_join: %s", strerror(ret
));
49 ret
= pthread_cond_destroy(&cond
);
50 if (ret
) errx(1, "pthread_cond_destroy: %s", strerror(ret
));
52 printf("CV successfully destroyed.\n");
54 ret
= pthread_mutex_destroy(&mutex
);
55 if (ret
) errx(1, "pthread_mutex_destroy: %s", strerror(ret
));
65 ret
= pthread_mutex_lock(&mutex
);
66 if (ret
) errx(1, "pthread_mutex_lock: %s", strerror(ret
));
68 pthread_cleanup_push(unlock
, &mutex
);
72 ret
= pthread_cond_broadcast(&cond
);
73 if (ret
) errx(1, "pthread_cond_broadcast: %s", strerror(ret
));
74 ret
= pthread_cond_wait(&cond
, &mutex
);
75 if (ret
) errx(1, "pthread_cond_wait: %s", strerror(ret
));
78 pthread_cleanup_pop(0);
79 ret
= pthread_mutex_unlock(&mutex
);
80 if (ret
) errx(1, "pthread_mutex_unlock: %s", strerror(ret
));
90 pthread_mutex_unlock((pthread_mutex_t
*)arg
);