Exclusive mode seems to work without deadlocks
[mirrors/Programs.git] / c / pthread_extra / test_pause.c
CommitLineData
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
9pthread_t main_thread;
10
a8e71e8f
TM
11void *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 15 pthread_nsleep(0, 1000*1000*300);
de59b17e 16 pthread_pause_all();
a8e71e8f 17 printf("Running%s!\n", (char *)arg);
de59b17e
TM
18 pthread_unpause_all();
19 pthread_pause(main_thread);
3109d0d1 20 pthread_unpause(main_thread);
a8e71e8f
TM
21 }
22}
23
24int 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();
de59b17e 68 //printf("\n");
3109d0d1 69 pthread_sleep(1);
9b4be8e8
TM
70 printf("SWITCH MAIN A+B:\n");
71 pthread_unpause_all();
de59b17e 72 //printf("\n");
3109d0d1 73 pthread_sleep(1);
a8e71e8f
TM
74 }
75
76 pthread_join(a, NULL);
77 pthread_join(b, NULL);
78 printf("DIEDED!\n");
79}
This page took 0.219023 seconds and 4 git commands to generate.