| 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 | pthread_pause(main_thread); |
| 18 | printf("Running%s!\n", (char *)arg); |
| 19 | //pthread_unpause_all(); |
| 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 | pthread_sleep(1); |
| 69 | printf("SWITCH MAIN A+B:\n"); |
| 70 | pthread_unpause_all(); |
| 71 | pthread_sleep(1); |
| 72 | } |
| 73 | |
| 74 | pthread_join(a, NULL); |
| 75 | pthread_join(b, NULL); |
| 76 | printf("DIEDED!\n"); |
| 77 | } |