4. Threads Synchronization

  1. Compile the following program, explain it. Is it working well?

            include pthread.h
    
              float hole= 0;
              int flag=0;
    
              /*
               *      Code of the threads
               */
              void delay() {
                 int aux2;
    
                 for (aux2=600000; aux2>0; aux2--);
                 return;
              }
    
              void delay2() {
                 int aux2;
    
                 for (aux2=100000; aux2>0; aux2--);
    
                return;
              }
    
              double get() {
                 double aux1;
    
                 aux1= hole;
                 return aux1;
              }
    
              double inc() {
    
                 int aux1;
                 for (aux1=0;aux1<10;aux1++) {
                    hole= hole + 0.100000000;
                    delay2();
                 }
                 return hole;
              }
    
              void* producer(int* name) {
    
                 int aux1, aux2, aux3, count;
    
    
                 /*   Main loop       */
                 for (aux1=0; aux1<20; aux1++) {
    
                    /*        Print name      */
    
                    printf("Producer inc: %.3f\n", inc());
    
                 }
                 pthread_exit(0);
              }
    
              void* consumer(int* name) {
    
                 int aux1, aux2, aux3;
    
                 /*   Main loop       */
                 for (aux1=0; aux1<10; aux1++) {
    
                    /*        Print name      */
                    printf("Consumer got: %.3f\n", get());
    
                    /*        Create delay    */
                    delay();
    
                 }
    
                pthread_exit(0);
              }
    
    
              void main(){
    
                 /*   Variables that keep the threads ids     */
                 pthread_t simpleThread1, simpleThread2;
    
                 int status, aux1, priority;
    
                 int pro1= 0;
                 int pro2= 1;
    
    
                 /*  Maximum concurrence level */
                 pthread_setconcurrency(100);
                 printf("Concurrency %d\n", pthread_getconcurrency());
    
                 /*   Create first thread     */
                 pthread_create(&simpleThread1, 0, producer, (void*) &pro1);
    
                 /*   Create first thread     */
                 pthread_create(&simpleThread2, 0, consumer, (void*) 0);
    
                 /*   Wait until all threads stop     */
                 pthread_join(simpleThread1, 0);
                 pthread_join(simpleThread2, 0);
       }
              

    To compile the program:

    cc threads.c -o threads -lpthread
  2. Add the following lines to the program. What happens? Explain.

    #include <pthread.h> 
    float hole= 0; 
    int flag=0; 
                           pthread_mutex_t lock_hole;
     ..
     double get() 
    {
     int aux1;
                           pthread_mutex_lock(&lock_hole);
     aux1= hole; 
                           pthread_mutex_unlock(&lock_hole); 
    return aux1; 
    } 
    double inc() 
    {
     int aux1; 
                           pthread_mutex_lock(&lock_hole); 
    for (aux1=0;aux1<10;aux1++) 
    { 
    hole= hole + 0.100000000; 
    delay2(); 
    } 
                           pthread_mutex_unlock(&lock_hole); 
    return hole; 
    } 
    ... void main()
    { 
    /* Variables that keep the threads ids */ 
    pthread_t simpleThread1, simpleThread2; 
    int status, aux1, priority; 
    int pro1= 0; 
    int pro2= 1; 
                           pthread_mutex_init(&lock_hole, 0); 
    
              
  3. Now add the following lines and explain what happens:

    #include <pthread.h> 
    float hole= 0;
    int flag=0; 
    pthread_mutex_t lock_hole; 
                          pthread_cond_t cond_hole;
     ... 
    double get() 
    { 
    int aux1; 
    pthread_mutex_lock(&lock_hole); 
                          pthread_cond_wait(&cond_hole, &lock_hole); 
    aux1= hole; 
    pthread_mutex_unlock(&lock_hole); 
    return aux1; 
    } 
    
    double inc() 
    { 
    int aux1; 
    pthread_mutex_lock(&lock_hole); 
    for (aux1=0;aux1<10;aux1++) 
    {
     hole= hole + 0.100000000; 
    delay2(); 
    }
                         pthread_cond_signal(&cond_hole); 
    pthread_mutex_unlock(&lock_hole); 
    return hole; 
    } 
    ... 
    void main()
    {
     /* Variables that keep the threads ids */ 
    pthread_t simpleThread1, simpleThread2; 
    int status, aux1, priority; 
    int pro1= 0; 
    int pro2= 1; 
    pthread_mutex_init(&lock_hole, 0); 
                         pthread_cond_init(&cond_hole, 0); 
    
              
  4. Run the following program and explain it:

    #include <pthread.h>
    
                    float hole= 0;
                    int flag=0;
                    pthread_mutex_t lock_hole;
                    pthread_cond_t  cond_hole;
    
                    /*
                     *      Code of the threads
                     */
                    void delay() {
                       int aux2;
    
                       for (aux2=1000000; aux2>0; aux2--);
                       return;
                    }
    
                    void delay2() {
                       int aux2;
    
                       for (aux2=300000; aux2 > 0; aux2--);
                       return;
                    }
    
                    double get() {
                       int aux1;
    
                       pthread_mutex_lock(&lock_hole);
                       pthread_cond_wait(&cond_hole, &lock_hole);
    
                       aux1= hole;
                       pthread_mutex_unlock(&lock_hole);
                       return aux1;
                    }
    
                    double inc() {
    
                       int aux1;
                       pthread_mutex_lock(&lock_hole);
                       for (aux1=0;aux1<10;aux1++) {
    
                          hole= hole + 0.100000000;
                          delay2();
                       }
                       pthread_cond_signal(&cond_hole);
                       pthread_mutex_unlock(&lock_hole);
                       return hole;
                    }
    
                    void* producer(int* name) {
    
                       int aux1, aux2, aux3, count;
    
    
                       /*   Main loop       */
                       for (aux1=0; aux1<20; aux1++) {
    
                          /*        Print name      */
                          printf("Producer%u inc: %.3f\n", *name, inc());
    
                       }
                       pthread_exit(0);
                    }
    
                    void* consumer(int* name) {
    
                       int aux1, aux2, aux3;
    
                       /*   Main loop       */
                       for (aux1=0; aux1<10; aux1++) {
    
                          /*        Print name      */
                          printf("Consumer got: %.3f\n", get());
    
                          /*        Create delay    */
                          delay();
    
                       }
                       pthread_exit(0);
                    }
    
                    void main(){
    
                       /*   Variables that keep the threads ids     */
                       pthread_t simpleThread1, simpleThread2;
    
                       int status, aux1, priority;
                       int pro1= 0;
                       int pro2= 1;
    
                       pthread_mutex_init(&lock_hole, 0);
                       pthread_cond_init(&cond_hole, 0);
    
                       /*  Maximum concurrence level */
                       printf("Concurrency %d\n", PTHREAD_THREADS_MAX);
    
                       /*   Create first thread     */
                       pthread_create(&simpleThread1, 0, producer, (void*)&pro1);
    
                       /*   Create first thread     */
                       pthread_create(&simpleThread2, 0, consumer, (void*) 0);
    
                       /*   Wait until all threads stop     */
                       pthread_join(simpleThread1, 0);
                       pthread_join(simpleThread2, 0);
                    }
    
              
  5. Note: Change the delays in the program and see what happens.