6 static void thread_cleanup(void *arg)
8 printf("cleaning up 0x%lx\n", (long)arg);
13 static void *thread_main(void *arg)
15 pthread_cleanup_push(thread_cleanup, (void *)0x1234);
16 pthread_cleanup_push(thread_cleanup, (void *)0x5678);
18 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
20 perror("pthread_setcanceltype");
24 if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
26 perror("pthread_setcancelstate");
32 pthread_cleanup_pop(0);
33 pthread_cleanup_pop(0);
38 int main(int argc, char **argv)
43 if (pthread_create(&tid, NULL, thread_main, NULL) != 0)
45 perror("pthread_create");
51 if (pthread_cancel(tid) != 0)
53 perror("pthread_cancel");
57 if (pthread_join(tid, &result) != 0)
59 perror("pthread_join");
63 printf("result is %s\n", result == PTHREAD_CANCELED ? "correct" : "incorrect");