notes16

notes16 - Condition Variables cond_init initializes the...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Condition Variables cond_init . initializes the condition variables cond_wait . waits until a cond_signal is called on this variable . cond. vars. have always a mutex associated with it . cond_wait has to be called in the following way mutex_lock(&cond_mutex); //do some stuff while(cond_expr) { cond_wait(&cond_var,&cond_mutex) } //do stuff while mutex is locked mutex_unlock(&cond_mutex); . cond_wait does the following: o cond_mutex is unlocked o thread goes to wait until there is a cond_signal o when it wakes up, cond_wait will lock the mutex again o cond_expr is always checked while mutex is locked
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cond_signal . wakes up a thread waiting for a condition var . cond_signal has to be called in the following way mutex_lock(&cond_mutex); //modifiy cond_expr and set it to false //do other stuff cond_signal(&cond_var); mutex_unlock(&cond_mutex); . the thread calling cond_wait will wakeup and it will check cond_expr again. If it’s true, it will go back to sleep calling cond_wait again. Otherwise, it will exit the while loop. . if there’s no thread waiting for condition var, condition_signal is ignored. Bounded Buffer using Cond Vars class BoundedBufferCondVars { int *_away; int _max; int _head; int _tail; int n; cond_t cond_empty; //threads calling remove wait here if queue is empty cond_t cond_full; //threads calling insert wait here if queue is full mutex_t cond_mutex;
Background image of page 2
public: BoundedBufferCondVars(int max); void insert(int val); int remove(); }; BoundedBufferCondVars::BoundedBufferCondVars(int max) { _array = new int[max]; assert(_array != NULL);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 13

notes16 - Condition Variables cond_init initializes the...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online