| 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_extra_create(&a, NULL, thread_test, " A"); |
| 24 | pthread_extra_create(&b, NULL, thread_test, " B"); |
| 25 | //sleep(1); |
| 26 | |
| 27 | //printf("OK\n"); |
| 28 | /* |
| 29 | for(int32_t i = 1;i>0;i++) { |
| 30 | pthread_pause(a); |
| 31 | pthread_pause(a); |
| 32 | pthread_unpause(a); |
| 33 | pthread_unpause(a); |
| 34 | } |
| 35 | */ |
| 36 | /* |
| 37 | exit(23); |
| 38 | pthread_pause(a); |
| 39 | pthread_unpause(b); |
| 40 | */ |
| 41 | |
| 42 | while(1) { |
| 43 | pthread_pause(b); |
| 44 | pthread_unpause(a); |
| 45 | printf("SWITCH A:\n"); |
| 46 | sleep(2); |
| 47 | |
| 48 | printf("SWITCH B:\n"); |
| 49 | pthread_pause(a); |
| 50 | pthread_unpause(b); |
| 51 | sleep(2); |
| 52 | |
| 53 | printf("SWITCH A+B:\n"); |
| 54 | pthread_unpause(a); |
| 55 | pthread_unpause(b); |
| 56 | sleep(1); |
| 57 | } |
| 58 | |
| 59 | pthread_join(a, NULL); |
| 60 | pthread_join(b, NULL); |
| 61 | printf("DIEDED!\n"); |
| 62 | } |