Commit | Line | Data |
---|---|---|
a8e71e8f TM |
1 | #include <signal.h> |
2 | #include <pthread.h> | |
3 | #include <pthread_extra.h> | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <assert.h> | |
7 | #include <unistd.h> | |
8 | ||
3109d0d1 TM |
9 | pthread_t main_thread; |
10 | ||
a8e71e8f TM |
11 | void *thread_test(void *arg) { |
12 | //Whole process dies if you kill thread immediately before it is pausable | |
13 | //pthread_pause_enable(); | |
14 | while(1) { | |
3109d0d1 TM |
15 | pthread_nsleep(0, 1000*1000*300); |
16 | //pthread_pause_all(); | |
17 | pthread_pause(main_thread); | |
a8e71e8f | 18 | printf("Running%s!\n", (char *)arg); |
3109d0d1 TM |
19 | //pthread_unpause_all(); |
20 | pthread_unpause(main_thread); | |
a8e71e8f TM |
21 | } |
22 | } | |
23 | ||
24 | int main() { | |
25 | ||
3109d0d1 TM |
26 | main_thread = pthread_self(); |
27 | ||
a8e71e8f TM |
28 | pthread_t a, b; |
29 | pthread_pause_enable(); //Will get inherited by all threads from now on | |
30 | //That way you can be sure it is pausable immediately | |
7e0f3dba TM |
31 | pthread_extra_create(&a, NULL, thread_test, " A"); |
32 | pthread_extra_create(&b, NULL, thread_test, " B"); | |
3109d0d1 | 33 | //pthread_sleep(1); |
24286ba9 TM |
34 | |
35 | //printf("OK\n"); | |
36 | /* | |
37 | for(int32_t i = 1;i>0;i++) { | |
38 | pthread_pause(a); | |
39 | pthread_pause(a); | |
40 | pthread_unpause(a); | |
41 | pthread_unpause(a); | |
42 | } | |
43 | */ | |
44 | /* | |
45 | exit(23); | |
46 | pthread_pause(a); | |
47 | pthread_unpause(b); | |
48 | */ | |
a8e71e8f TM |
49 | |
50 | while(1) { | |
51 | pthread_pause(b); | |
52 | pthread_unpause(a); | |
53 | printf("SWITCH A:\n"); | |
3109d0d1 | 54 | pthread_sleep(2); |
a8e71e8f TM |
55 | |
56 | printf("SWITCH B:\n"); | |
57 | pthread_pause(a); | |
58 | pthread_unpause(b); | |
3109d0d1 | 59 | pthread_sleep(2); |
a8e71e8f TM |
60 | |
61 | printf("SWITCH A+B:\n"); | |
62 | pthread_unpause(a); | |
63 | pthread_unpause(b); | |
3109d0d1 | 64 | pthread_sleep(1); |
9b4be8e8 TM |
65 | |
66 | printf("SWITCH MAIN ONLY:\n"); | |
67 | pthread_pause_all(); | |
3109d0d1 | 68 | pthread_sleep(1); |
9b4be8e8 TM |
69 | printf("SWITCH MAIN A+B:\n"); |
70 | pthread_unpause_all(); | |
3109d0d1 | 71 | pthread_sleep(1); |
a8e71e8f TM |
72 | } |
73 | ||
74 | pthread_join(a, NULL); | |
75 | pthread_join(b, NULL); | |
76 | printf("DIEDED!\n"); | |
77 | } |