70 likes | 274 Views
Monitors. Controls. Condition variables Value Queue ONLY used in a monitor Signal "promotes" or "de-queues" a waiter Two types Signal & exit (C.A.R. Hoare) Signal & continue (Per Brinch Hansen-Mesa). Signal & Continue. Waiting thread still waits for actual exit
E N D
Controls • Condition variables • Value • Queue • ONLY used in a monitor • Signal "promotes" or "de-queues" a waiter • Two types • Signal & exit (C.A.R. Hoare) • Signal & continue (Per Brinch Hansen-Mesa)
Signal & Continue • Waiting thread still waits for actual exit • Allows system to 'prepare' the waiter • Used in Java
Inside the Monitor • May want to wait for something • Condition variable controls the wait • While condwait thread is moved outside the monitor
Sample – acts like a semaphore boolean busy = false; Condition avail; MonitorEntry void getRes() // same as "P" {if (busy) wait(avail); // put me in queue busy=true; // now held } MonitorEntry void freeRes() // same as "V" {busy=false; signal (avail); }
Readers & Writers Condition canwrite, canread; boolean writelock=false; int rdrs=0; monitorEntry void beginread() {if (writelock || queue(canwrite)) wait canread; ++rdrs; signal (canread); } MonitorEntry void endread() {--rdrs; if (rdrs==0) signal(canwrite); } monitorEntry void beginWrite() {if (rdrs>0 || writelock) wait (canwrite); writelock=true; } monitorEntry void endwrite() {writelock=false; if (queue(canread)) signal(canread); else signal(canwrite); }