c - Semaphore Vs Condition Variables in multithreading? -


problem: have increment x1 , x2 variable should done separate threads , next increment of both variables should not called until previous increment of both variable not completed.

problem:

x1 =  0;  x2 = 0;  x1++; , x2++; should run parallel on different threads , wait printing. should print: x1 = 1 , x2 = 1  x1++; , x2++; should run parallel on different threads , wait printing. should print: x1 = 2 , x2 = 2  x1++; , x2++; should run parallel on different threads , wait printing. should print: x1 = 3 , x2 = 3  x1++; , x2++; should run parallel on different threads , wait printing. should print: x1 = 4 , x2 = 4     …     …     … x1++; , x2++; should run parallel on different threads , wait printing. should print: x1 = 10 , x2 = 10  close threads 

proposed solution using pthread condition: initialize 4 mutexes , 4 condition variables, , use 2 mutextes lock main function , rest each each thread. both of threads wait main function pass condtion signal invoke them , after calculation, passing signal main thread move further. sleep of 1 sec provided asking threads invoke , ready recieve , return signal after calculation.

pthread condition code:

    #include <stdio.h>     #include <pthread.h>      pthread_t pth1,pth2;     //values calculate     int x1 = 0, x2 = 0;      pthread_mutex_t m1, m2, m3, m4 = pthread_mutex_initializer;     pthread_cond_t c1, c2, c3, c4 = pthread_cond_initializer;      void *threadfunc1(void *parm)     {         pthread_mutex_lock(&m1);         (;;) {             pthread_cond_wait(&c1, &m1);             x1++;             pthread_mutex_lock(&m3);             pthread_cond_signal(&c3);             pthread_mutex_unlock(&m3);         }         pthread_mutex_unlock(&m1);         return null ;     }     void *threadfunc2(void *parm)     {         pthread_mutex_lock(&m2);         (;;) {             pthread_cond_wait(&c2, &m2);             x2++;             pthread_mutex_lock(&m4);             pthread_cond_signal(&c4);             pthread_mutex_unlock(&m4);         }         pthread_mutex_unlock(&m2);         return null ;     }      int main () {         pthread_create(&pth1, null, threadfunc1, "foo");         pthread_create(&pth2, null, threadfunc2, "foo");         sleep(1);         int loop = 0;         pthread_mutex_lock(&m3);         pthread_mutex_lock(&m4);         while (loop < 10) {             // iterated step             loop++;             printf("initial   : x1 = %d, x2 = %d\n", x1, x2);             pthread_mutex_lock(&m1);             pthread_cond_signal(&c1);             pthread_mutex_unlock(&m1);             pthread_mutex_lock(&m2);             pthread_cond_signal(&c2);             pthread_mutex_unlock(&m2);             pthread_cond_wait(&c3, &m3);             pthread_cond_wait(&c4, &m4);             printf("final   : x1 = %d, x2 = %d\n", x1, x2);         }         printf("result   : x1 = %d, x2 = %d\n", x1, x2);         pthread_mutex_unlock(&m3);         pthread_mutex_unlock(&m4);         pthread_cancel(pth1);         pthread_cancel(pth2);         return 1;     } 

proposed solution using semaphore: initialize 4 semaphore , invoke separate threads separate increment of variable. 2 semaphores passing message threads start incrementing , 2 semaphores passing message main thread incrementation completed. main thread wait semaphore posting both child threads showing incrementation of both variable done, main thread pass message both child threads allowing further incrementing.

semaphore code:

    #include <stdio.h>     #include <pthread.h>     #include <semaphore.h>      //threads     pthread_t pth1,pth2;      //values calculate     int x1 = 0, x2 = 0;      sem_t c1,c2,c3,c4;      void *threadfunc1(void *parm)     {         (;;) {             x1++;             sem_post(&c1);             sem_wait(&c3);         }         return null ;     }      void *threadfunc2(void *parm)     {         (;;) {             x2++;             sem_post(&c2);             sem_wait(&c4);         }         return null ;     }      int main () {         sem_init(&c1, 0, 0);         sem_init(&c2, 0, 0);         sem_init(&c3, 0, 0);         sem_init(&c4, 0, 0);         pthread_create(&pth1, null, threadfunc1, "foo");         pthread_create(&pth2, null, threadfunc2, "foo");         sem_wait(&c1);         sem_wait(&c2);         sem_post(&c3);         sem_post(&c4);         int loop = 0;         while (loop < 8) {             // iterated step             loop++;             printf("initial   : x1 = %d, x2 = %d\n", x1, x2);             sem_wait(&c1);             sem_wait(&c2);             printf("final   : x1 = %d, x2 = %d\n", x1, x2);             sem_post(&c3);             sem_post(&c4);         }         sem_wait(&c1);         sem_wait(&c2);         sem_destroy(&c1);         sem_destroy(&c2);         sem_destroy(&c3);         sem_destroy(&c4);         printf("result   : x1 = %d, x2 = %d\n", x1, x2);         pthread_cancel(pth1);         pthread_cancel(pth2);         return 1;     } 

please suggest me, 1 better way implement or can improve? kind of suggestions helpful. in advance. , sorry, if repeating myself, because, problem has become nightmare me.. please help.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -