47f2db0cc20b0cf48bb17284cd1fab5891673676
[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 void *thread_test(void *arg) {
10 //Whole process dies if you kill thread immediately before it is pausable
11 //pthread_pause_enable();
12 while(1) {
13 usleep(1000*300);
14 printf("Running%s!\n", (char *)arg);
15 }
16 }
17
18 int main() {
19
20 pthread_t a, b;
21 pthread_pause_enable(); //Will get inherited by all threads from now on
22 //That way you can be sure it is pausable immediately
23 pthread_create(&a, NULL, thread_test, " A");
24 pthread_create(&b, NULL, thread_test, " B");
25
26 while(1) {
27 pthread_pause(b);
28 pthread_unpause(a);
29 printf("SWITCH A:\n");
30 sleep(2);
31
32 printf("SWITCH B:\n");
33 pthread_pause(a);
34 pthread_unpause(b);
35 sleep(2);
36
37 printf("SWITCH A+B:\n");
38 pthread_unpause(a);
39 pthread_unpause(b);
40 sleep(1);
41 }
42
43 pthread_join(a, NULL);
44 pthread_join(b, NULL);
45 printf("DIEDED!\n");
46 }
This page took 0.296759 seconds and 3 git commands to generate.