Condition Variable POSIX Thread : C/C++ -
i learning multithreading. regard
http://www.yolinux.com/tutorials/linuxtutorialposixthreads.html#scheduling
#include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = pthread_mutex_initializer; pthread_cond_t condition_var = pthread_cond_initializer; void *functioncount1(); void *functioncount2(); int count = 0; #define count_done 10 #define count_halt1 3 #define count_halt2 6 main() { pthread_t thread1, thread2; pthread_create( &thread1, null, &functioncount1, null); pthread_create( &thread2, null, &functioncount2, null); pthread_join( thread1, null); pthread_join( thread2, null); printf("final count: %d\n",count); exit(0); } // write numbers 1-3 , 8-10 permitted functioncount2() void *functioncount1() { for(;;) { // lock mutex , wait signal relase mutex pthread_mutex_lock( &count_mutex ); // wait while functioncount2() operates on count // mutex unlocked if condition varialbe in functioncount2() signaled. pthread_cond_wait( &condition_var, &count_mutex ); count++; printf("counter value functioncount1: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= count_done) return(null); } } // write numbers 4-7 void *functioncount2() { for(;;) { pthread_mutex_lock( &count_mutex ); if( count < count_halt1 || count > count_halt2 ) { // condition of if statement has been met. // signal free waiting thread freeing mutex. // note: functioncount1() permitted modify "count". pthread_cond_signal( &condition_var ); } else { count++; printf("counter value functioncount2: %d\n",count); } pthread_mutex_unlock( &count_mutex ); if(count >= count_done) return(null); } }
i want know control flow of code.
as pthread_cond_wait - unlocks mutex , waits condition variable cond signaled
what understood control of flow is
1) thread one,two created , thread1 passed control (considering single core processor system)
2) when encounters pthread_cond_wait( &condition_var, &count_mutex );
in thread1
routine void *functioncount1()
- releases lock , goes wait state passing control thread2 void *functioncount1()
3) in thread2
variable count
checked , since satisfies count < count_halt1 || count > count_halt2
- signals thread1
, restarts increment count
4) steps 2 3
repeated displays 1-3
thread1
5) count 4-7
thread2 in action , there no switching between thread1
, thread2
6)for count 8-10
again steps 2-3
repeated.
i want know whether understanding correct? thread1
goes sleep , thread2
wakes ( i.e threads switched) count value 1-3 , 8-10
i.e switching between threads happen 5 times
?
edit
my main concern ask question know if thread1
go sleep
state when encounters pthread_cond_wait( &condition_var, &count_mutex );
, won't active
again unless signalled thread2
, increments count
i.e. not going increment 1-3
in 1 go rather each increment , has wait signal thread2
can proceed further
1) threads created. control not passed thread1, it's system scheduler decides thread execute. both threads active, both should receive processor time, order not determined. there might several context switches, don't control this.
2) correct, thread1 comes waiting state, thread2 continues working. again, control not passed explicitly.
3) yes, thread2 notifies condition variable, thread1 awake , try reacquire mutex. control not go thread1.
in general should understand can't control thread execute; it's job os system scheduler, can put many context switches wants.
upd: condition variables can control order of tasks execution within multithreading environment. think understanding more or less correct: thread1 waiting on condition variable signal thread2. when signal received thread1 continues execution (after reacquires mutex). switching between threads - there might many of them, 5 theoretical minimum.
Comments
Post a Comment