Lecture 13 Condition Variables
Contents In this lecture, you will learn Condition Variables And how to use CVs to solve The Producer/Consumer (Bounded Buffer) Problem
Review Thus far we have developed the notion of a lock. Unfortunately, locks are not the only primitives that are needed to build concurrent programs. E.g., one thread waiting for another, remember pthread_join()?
Review Two problems:
Review Using a shared variable? Is it correct? Yes What is the limitation? Waste of CPU time Do we need a lock here? Not a critical section
Condition Variables A condition variable is an explicit queue that: threads can put themselves on when some state of execution (i.e., some condition) is not as desired (by waiting on the condition); some other thread, when it changes said state, can then wake one (or more) of those waiting threads and thus allow them to continue (by signaling on the condition).
Condition Variables The POSIX calls: The wait() call is executed when a thread wishes to put itself to sleep; The signal() call is executed when a thread has changed something in the program and thus wants to wake a sleeping thread waiting on this condition
Condition Variables
Condition Variables Discussion: Why do we need a lock here? Why we need the variable &m in calling wait()?
Discussion Why is the code broken?
Discussion Why is the code broken?
Contents In this lecture, you will learn Condition Variables And how to use CVs to solve The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem Imagine one or more producer threads and one or more consumer threads. Producers generate data items and place them in a buffer; Consumers grab said items from the buffer and consume them in some way. (A bounded buffer is used when you pipe the output of one program into another.)
The Producer/Consumer (Bounded Buffer) Problem Let s begin with only 1 producer, 1 consumer, buffer-size = 1:
The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem The put() and get() routines have critical sections within them.
The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem With just a single producer and a single consumer, the code works. However, if we have more than one of these threads (e.g., two consumers), the solution has two critical problems.
The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem A simple rule to remember with condition variables is to always use while loops.
The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem Discussion: Dealing with bounded buffer instead of single buffer. How many CVs do we need?
The Producer/Consumer (Bounded Buffer) Problem
The Producer/Consumer (Bounded Buffer) Problem
Discussion Let us make the Producer/Consumer Problem more complicated. Different producers generate different types of data items and place them in a buffer; Picky consumers each grab some certain type(s) of items from the buffer and consume them in some way. How many CVs do we need? It depends An example on memory allocation.
Discussion Let us make the Producer/Consumer Problem more complicated. Different producers generate different types of data items and place them in a buffer; Picky consumers each grab some certain type(s) of items from the buffer and consume them in some way. How many CVs do we need? It depends An example on memory allocation.
Discussion
Problem Assume there are zero bytes free; thread Ta calls allocate(100), followed by thread Tb which asks for less memory by calling allocate(10). At that point, assume a third thread, Tc, calls free(50). If Ta is woken up, both threads will keep waiting. The code does not work, as the thread waking other threads does not know which thread (or threads) to wake up.
Covering Condition Many CVs? Based on the types of items. One CV is enough. Replace the pthread_cond_signal() call in the code above with a call to pthread_cond_broadcast(), which wakes up all waiting threads.
Summary Condition Variables The Producer/Consumer (Bounded Buffer) Problem