Ti new thread dummy do something with it ti create

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: // create N threads S-> P(); P(); for (i=1;i<n: i++) { // got resource! t[i] = new Thread (“dummy”); // do something with it t[i]->create (fn); S->V(); } … … } Locks Locks Locks can be used to ensure mutual exclusion in conflicting critical critical sections. • A lock is an object, a data item in memory. Methods: Lock::Acquire and Lock::Release. • • • • • • • A Thread pair calls to Acquire and Release. A Acquire before entering a critical section. R Release after leaving a critical section. R Between Acquire/Release, the lock is held. Acquire does not return until any previous holder releases. Waiting locks can spin (a spinlock) or block (a mutex). Mutex can be easily implemented by a binary semaphore Synchronization must be programmed carefully …. Condition Condition Variables Condition variables are a synchronization construct that provides provides several simple operations • The operation wait means that the process invoking this operation is suspended until another process invokes signal • The signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. • Additional operation is broadcast which wakes up all suspended processes/threads Condition Condition Variables Condition variables allow explicit event notification. Different interface from the book …. • associated with a mutex to prevent races on event conditions Condition::Wait(Lock*) Called with lock held: sleep, atomically releasing lock. Atomically reacquire lock before returning. Condition:: Signal(Lock*) Wake up one waiter, if any. Condition::Broadcast(Lock*) Wake up all waiters, if any. Semaphores Semaphores vs. Condition Variables Fundamental difference implicit vs. explicit synchronization conditions CV: lock arbitrary code if arbitrary condition … wait or signal … unlock Semaphore: if if arbitrary condition P() or V()? lock if arbitrary condition P() P() or V()? unlock Semaphores Semaphores vs. Condition Variables 1. V differs from Signal in that: • Signal has no effect if no thread is waiting on the condition. Condition variables are not variables! They have no value! • V has the same effect whether or not a thread is waiting. Semaphores retain a “memory” of calls to V. 2. P differs from Wait in that: • P checks the condition and blocks only if necessary. no need to recheck the condition after returning from P wait condition is defined internally, but is limited to a counter • Wait is explicit: it does not check the condition, ever. condition is defined externally and protected by integrated mutex BREAK BREAK CSci CSci 5103 Operating Systems Advanced Synchronization Barriers Barriers This is an important...
View Full Document

This document was uploaded on 04/02/2014.

Ask a homework question - tutors are online