Exclusive mode seems to work without deadlocks
[mirrors/Programs.git] / c / pthread_extra / test_pause.c
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
9 pthread_t main_thread;
10
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) {
15 pthread_nsleep(0, 1000*1000*300);
16 pthread_pause_all();
17 printf("Running%s!\n", (char *)arg);
18 pthread_unpause_all();
19 pthread_pause(main_thread);
20 pthread_unpause(main_thread);
21 }
22 }
23
24 int main() {
25
26 main_thread = pthread_self();
27
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
31 pthread_extra_create(&a, NULL, thread_test, " A");
32 pthread_extra_create(&b, NULL, thread_test, " B");
33 //pthread_sleep(1);
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 */
49
50 while(1) {
51 pthread_pause(b);
52 pthread_unpause(a);
53 printf("SWITCH A:\n");
54 pthread_sleep(2);
55
56 printf("SWITCH B:\n");
57 pthread_pause(a);
58 pthread_unpause(b);
59 pthread_sleep(2);
60
61 printf("SWITCH A+B:\n");
62 pthread_unpause(a);
63 pthread_unpause(b);
64 pthread_sleep(1);
65
66 printf("SWITCH MAIN ONLY:\n");
67 pthread_pause_all();
68 //printf("\n");
69 pthread_sleep(1);
70 printf("SWITCH MAIN A+B:\n");
71 pthread_unpause_all();
72 //printf("\n");
73 pthread_sleep(1);
74 }
75
76 pthread_join(a, NULL);
77 pthread_join(b, NULL);
78 printf("DIEDED!\n");
79 }
This page took 0.273136 seconds and 4 git commands to generate.