]>
Commit | Line | Data |
---|---|---|
964d3577 A |
1 | #define __DARWIN_NON_CANCELABLE 0 |
2 | #include <pthread.h> | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <unistd.h> | |
6 | ||
7 | static pthread_once_t once = PTHREAD_ONCE_INIT; | |
8 | static int x = 0; | |
9 | ||
10 | void cancelled(void) | |
11 | { | |
12 | printf("thread cancelled.\n"); | |
13 | } | |
14 | ||
15 | void oncef(void) | |
16 | { | |
17 | printf("in once handler: %p\n", pthread_self()); | |
18 | sleep(5); | |
19 | x = 1; | |
20 | } | |
21 | ||
22 | void* a(void *ctx) | |
23 | { | |
24 | printf("a started: %p\n", pthread_self()); | |
25 | pthread_cleanup_push((void*)cancelled, NULL); | |
26 | pthread_once(&once, oncef); | |
27 | pthread_cleanup_pop(0); | |
28 | printf("a finished\n"); | |
29 | return NULL; | |
30 | } | |
31 | ||
32 | void* b(void *ctx) | |
33 | { | |
34 | sleep(1); // give enough time for a() to get into pthread_once | |
35 | printf("b started: %p\n", pthread_self()); | |
36 | pthread_once(&once, oncef); | |
37 | printf("b finished\n"); | |
38 | return NULL; | |
39 | } | |
40 | ||
41 | int main(void) | |
42 | { | |
43 | pthread_t t1; | |
44 | if (pthread_create(&t1, NULL, a, NULL) != 0) { | |
45 | fprintf(stderr, "failed to create thread a."); | |
46 | exit(1); | |
47 | } | |
48 | ||
49 | pthread_t t2; | |
50 | if (pthread_create(&t2, NULL, b, NULL) != 0) { | |
51 | fprintf(stderr, "failed to create thread b."); | |
52 | exit(1); | |
53 | } | |
54 | ||
55 | sleep(2); | |
56 | pthread_cancel(t1); | |
57 | ||
58 | pthread_join(t1, NULL); | |
59 | pthread_join(t2, NULL); | |
60 | exit(0); | |
61 | } |