notes16 - Condition Variables cond_init initializes the...

Info icon This 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
Image of page 1

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

View Full Document Right 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;
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);
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern