CPSC 3220 - DAY 9 SEPTEMBER 28, 2017 ================================================================================ Lock is initially free Always acquire before accessing shared data structure at beginning. Always release after finishing with shared data -End of procedure -Only the lock holder can release -Do not throw lock for someone else to release Never access shared data without lock Bounded Buffer -------------- For simplicity, assume no wraparound on the integers front and last -Front = total number of items that have ever been removed; tail = total number of items ever inserted Locks at beginning of procedure; unlock at end; no access of locks Note that we don't know whether the buffer is still empty once we release the lock - we only know the state of the buffer while holding the lock Condition Variables -Waiting inside a critical section -Wait: atomically release lock and relinquish processor -Reacquire the lock when wakened -Signal: wake up a waiter, if any -Broadcast: wake up all waiters, if any. -Always hold locke when calling wait, signal, broadcast -Condition variable is sync FOR shared state -ALWAYS hold lock when accessing shared state -Condition variable is memoryless -If signal when no one is waiting no op -If wait before signal, waiter wakes up -Wait atomically releases lock