Exclusive mode seems to work without deadlocks
[mirrors/Programs.git] / c / pthread_extra / test_pause.c
index 47f2db0cc20b0cf48bb17284cd1fab5891673676..b99c388c735cc3ac0579fcace9f6cfa815a4df7f 100644 (file)
@@ -6,38 +6,71 @@
 #include <assert.h>
 #include <unistd.h>
 
+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_create(&a, NULL, thread_test, " A");
-       pthread_create(&b, NULL, thread_test, " B");
+       pthread_extra_create(&a, NULL, thread_test, " A");
+       pthread_extra_create(&b, NULL, thread_test, " B");
+       //pthread_sleep(1);
+
+       //printf("OK\n");
+       /*
+       for(int32_t i = 1;i>0;i++) {
+               pthread_pause(a);
+               pthread_pause(a);
+               pthread_unpause(a);
+               pthread_unpause(a);
+       }
+       */
+       /*
+       exit(23);
+       pthread_pause(a);
+       pthread_unpause(b);
+       */
 
        while(1) {
                pthread_pause(b);
                pthread_unpause(a);
                printf("SWITCH A:\n");
-               sleep(2);
+               pthread_sleep(2);
 
                printf("SWITCH B:\n");
                pthread_pause(a);
                pthread_unpause(b);
-               sleep(2);
+               pthread_sleep(2);
 
                printf("SWITCH A+B:\n");
                pthread_unpause(a);
                pthread_unpause(b);
-               sleep(1);
+               pthread_sleep(1);
+
+               printf("SWITCH MAIN ONLY:\n");
+               pthread_pause_all();
+               //printf("\n");
+               pthread_sleep(1);
+               printf("SWITCH MAIN A+B:\n");
+               pthread_unpause_all();
+               //printf("\n");
+               pthread_sleep(1);
        }
 
        pthread_join(a, NULL);
This page took 0.119715 seconds and 4 git commands to generate.