Condition Variables
Condition Variables
Condition Variables
Don Porter
Portions courtesy Emmett Witchel
1
COMP 530: Operating Systems
Synchronization
• Now that you have seen locks, is that all there is?
• Compromises:
– between making it easy to modify shared variables
AND
– restricting when you can modify shared variables.
– between really flexible primitives AND
– simple primitives that are easy to reason about.
COMP 530: Operating Systems
Beyond Locking
• Locks ensure mutual exclusion
• Bounded Buffer problem – producer puts things in
a fixed sized buffer, consumer takes them out.
– Synchronizing on a condition.
Class BoundedBuffer{
…
void* buffer[];
pthread_mutex_t lock; What is wrong
int count = 0; with this?
}
BoundedBuffer::Deposit(c){ BoundedBuffer::Remove(c){
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
while (count == n); //spin while (count == 0); // spin
Add c to the buffer; Remove c from buffer;
count++; count--;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
COMP 530: Operating Systems
Beyond Locks
Class BoundedBuffer{
…
void* buffer[];
pthread_lock_t lock;
int count = 0; What is wrong
} with this?
BoundedBuffer::Deposit(c){ BoundedBuffer::Remove(c){
while (count == n); //spin while (count == 0); // spin
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
Add c to the buffer; Remove c from buffer;
count++; count--;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
COMP 530: Operating Systems
Beyond Locks
Class BoundedBuffer{
…
void* buffer[];
pthread_lock_t lock; What is wrong
int count = 0; with this?
}
BoundedBuffer::Deposit(c){ BoundedBuffer::Remove(c){
if (count == n) sleep(); if (count == 0) sleep();
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
Add c to the buffer; Remove c from buffer;
count++; count--;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
if(count == 1) wakeup(remove); if(count==n-1) wakeup(deposit);
} }
COMP 530: Operating Systems
Beyond Locks
Class BoundedBuffer{
…
void* buffer[];
pthread_lock_t lock; What is wrong
int count = 0; with this?
}
BoundedBuffer::Deposit(c){ BoundedBuffer::Remove(c){
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
if (count == n) sleep(); if (count == 0) sleep();
Add c to the buffer; Remove c from buffer;
count++; count--;
if(count == 1) wakeup(remove); if(count==n-1) wakeup(deposit);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
COMP 530: Operating Systems
Beyond Locks
Class BoundedBuffer{
… What is wrong
void* buffer[];
with this?
pthread_lock_t lock;
int count = 0;
}
BoundedBuffer::Remove(c){
BoundedBuffer::Deposit(c){ while(1) {
while(1) { pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock); if (count == 0) {
if(count == n) { pthread_mutex_unlock(&lock);
pthread_mutex_unlock(&lock); continue;
continue;} }
Add c to the buffer; Remove c from buffer;
count++; count--;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
break; break;
}} }}
COMP 530: Operating Systems
• Implementation
– Requires a per-condition variable queue to be maintained
– Threads waiting for the condition wait for a notify()
COMP 530: Operating Systems
• Requirements
– Only a single person can access the machine at any time
– If the machine is out of coke, wait until coke is restocked
– If machine is full, wait for consumers to drink coke prior to restocking
CokeMachine::Deposit(){ CokeMachine::Remove(){
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
while (count == n) { while (count == 0) {
pthread_cond_wait(¬Full, pthread_cond_wait(¬Empty,
&lock); } &lock); }
Add coke to the machine; Remove coke from to the machine;
count++; count--;
pthread_cond_notify(¬Empty); pthread_cond_notify(¬Full);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
COMP 530: Operating Systems
Summary
• Non-deterministic order of thread execution è concurrency problems
– Multiprocessing
• A system may contain multiple processors è cooperating threads/processes
can execute simultaneously
– Multi-programming
• Thread/process execution can be interleaved because of time-slicing
• Goal: Ensure that your concurrent program works under ALL possible
interleaving