X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Ftest_pause.c;h=c12de5a36bc614ff960c862601706593dc7e1b12;hb=a5d51291b687b5c0a599c918f39da5f73ad5a096;hp=ed60fcc6d187ae32501f00378033f512921ba8c8;hpb=9b4be8e8c190f7734a7611c2f354e2616e0f2c2f;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/test_pause.c b/c/pthread_extra/test_pause.c index ed60fcc..c12de5a 100644 --- a/c/pthread_extra/test_pause.c +++ b/c/pthread_extra/test_pause.c @@ -1,3 +1,5 @@ +#define PTHREAD_FREEWHEEL + #include #include #include @@ -6,23 +8,33 @@ #include #include +//#define printf(...) (0) + +pthread_t main_thread; + void *thread_test(void *arg) { //Whole process dies if you kill thread immediately before it is pausable //pthread_pause_enable(); while(1) { - usleep(1000*300); + pthread_nsleep(0, 1000*1000*300); + pthread_pause_all(); printf("Running%s!\n", (char *)arg); + pthread_unpause_all(); + pthread_pause(main_thread); + pthread_unpause(main_thread); } } int main() { + main_thread = pthread_self(); + pthread_t a, b; pthread_pause_enable(); //Will get inherited by all threads from now on //That way you can be sure it is pausable immediately pthread_extra_create(&a, NULL, thread_test, " A"); pthread_extra_create(&b, NULL, thread_test, " B"); - //sleep(1); + //pthread_sleep(1); //printf("OK\n"); /* @@ -42,25 +54,42 @@ int main() { while(1) { pthread_pause(b); pthread_unpause(a); + pthread_pause_disable(); + pthread_pause_disable(); + pthread_pause_disable(); printf("SWITCH A:\n"); - sleep(2); + pthread_pause_enable(); + pthread_pause_enable(); + pthread_pause_enable(); + pthread_sleep(2); + pthread_pause_all(); printf("SWITCH B:\n"); + pthread_unpause_all(); pthread_pause(a); pthread_unpause(b); - sleep(2); + pthread_sleep(2); + //pthread_pause_disable(); + pthread_pause_all(); printf("SWITCH A+B:\n"); + pthread_unpause_all(); + //pthread_pause_enable(); pthread_unpause(a); pthread_unpause(b); - sleep(1); + pthread_sleep(1); + //pthread_pause_disable(); + pthread_pause_all(); printf("SWITCH MAIN ONLY:\n"); + //pthread_pause_enable(); pthread_pause_all(); - sleep(1); + //printf("\n"); + pthread_sleep(1); printf("SWITCH MAIN A+B:\n"); pthread_unpause_all(); - sleep(1); + //printf("\n"); + pthread_sleep(1); } pthread_join(a, NULL);