DOC PREVIEW
U of I CS 241 - Conditional Variables and Mutex

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 Reserved2ContentsMonitorsConditional 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 Variablesvar 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 Variablesx.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 Reserved6OptionsProcess performing signal can eitherWait 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

U of I CS 241 - Conditional Variables and Mutex

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Conditional Variables and Mutex
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Conditional Variables and Mutex and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Conditional Variables and Mutex 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?