Conditional Variables and MutexContentsMonitorsMonitor with Condition VariablesSlide 5OptionsInitializationDestructionConditional Wait on Conditional VariableConditional Signal on Conditional VariableSignallingModel of Conditional VariableSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Unblocking Threads on ConditionsExampleSlide 21Slide 22Notes on Conditional VariablesDiscussionBarriersSlide 26Example with BarriersSlide 28Best Practices with Condition VariablesSummary01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved1Conditional Variables and MutexCS 241 Lecture 11R: Ch13.4 p465-472T: Ch 2.3 pp 115-124 Roy Campbell01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsMonitorsConditional Variables01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved3Monitorstype monitor-name = monitor variable declaration procedure entry P1(..); {... }; ...... procedure entry Pn(..); {...}; begin initialization code end01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved4Monitor with Condition Variablesvar x:condition; introduces a form of synchronization primitive A queue is associated with the condition x01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved5Monitor with Condition Variablesx.wait suspends the process until condition x is signaled; x.signal resumes one process awaiting condition x If no process is waiting, then this is a noop.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved6OptionsProcess performing signal can eitherWait until awoken process leaves monitor (or waits) in monitor (Hoare)Continue and completes (or waits in) monitor THEN awoken process continues01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Initialization#include <pthread.h>Dynamic initialization (attr is a conditional variable attribute variable- can use NULL to get default):int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);Static initialization:Pthread_cont_t_cond = PTHREAD_COND_INITIALIZER;------------------------------------------------------Pthread_cond_t barrier;int error;if (error = pthread_cond_init(&barrier, NULL); fprintf(stderr, “Failed to initialize barrier:%s\n”, strerror(error));01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Destruction#include <pthread.h>Dynamic destroy:int pthread_cond_destroy(pthread_cond_t *restrict cond);------------------------------------------------------pthread_cond_t tcond;if (error = pthread_cond_destroy(&tcond)) fprintf(stderr, “Failed to initialize tcond:%s\n”, strerror(error));01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved9Conditional Wait on Conditional Variablepthread_mutex_lock(&m);while (x != y) pthread_cond_wait(&v, &m);/* modify x or y if necessary */pthread_mutex_unlock(&m);The function pthread_cond_wait should only be called by a thread that owns the mutex, and the thread owns the mutex again when the function returns.While the thread is waiting, other threads may own the mutex.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved10Conditional Signal on Conditional Variablepthread_mutex_lock(&m);X++; pthread_cond_signal(&v);/* modify x or y if necessary */pthread_mutex_unlock(&m);The function pthread_cond_signal on a conditional variable wakes up one of the threads (if any) waiting on the conditional variable as soon as this thread unlocks the mutex.The thread waiting will own the mutex after it returns from wait.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved11M1SignallingM1M1cond_wait(&V1,&M1)cond_signal(&V1)Thread 1Thread 2After signal, when Thread 2 leaves M1, Thread 1 wakes up and owns M101/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexWait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved13Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexWait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved14Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexWait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved15Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexSIGNALWait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved16Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexSIGNAL(&V)Wait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved17Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutexconditional_wait(&V)Leave MutexWait to get in Mutex – Queue of threadsCondition Vimplemented as queue of threads01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved18Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutex – Queue of threadsconditional_wait(&V)Condition Vimplemented as queue of threadsLeave Mutex01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved19Unblocking Threads on Conditions#include <pthread.h>int pthread_cond_broadcast(pthread_cond_t *cond);Places all threads waiting on condition on mutex queueint pthread_cond_signal (pthread_t *cond);Places “at least one” thread waiting on condition on mutex queue01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved20Examplepthread_mutex_lock(&mutex);while (clock < deadline) pthread_cond_wait(&sim-time, &mutex);pthread_mutex_unlock(&mutex);pthread_mutex_lock(&mutex);clock = clock+increment; pthread_cond_broadcast(&sim-time);pthread_mutex_unlock(&mutex);01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved21Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutex – Queue of threadsconditional_wait(&sim_time)Condition sim_timeimplemented as queue of threadsLeave Mutex“broadcast”01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved22Model of Conditional VariableMutex CodeOutside MutexWait to get in Mutex – Queue of threadsconditional_wait(&sim_time)Condition sim_timeimplemented as queue of threadsLeave Mutex01/14/19 CS241 © 2005 Roy Campbell,
View Full Document