06.Locking - MutualExclusion: Primitivesand...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Mutual Exclusion: Primitives and  Implementation Considerations
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Too Much Milk: Lessons Software solution (Peterson’s algorithm) works, but it is unsatisfactory Solution is complicated; proving correctness is tricky even for the simple example While thread is waiting, it is consuming CPU time Asymmetric solution exists for 2 processes. How can we do better? Use hardware features to eliminate busy waiting Define higher-level programming abstractions to simplify concurrent programming
Background image of page 2
3 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there? Initially, X == 0. void increment() { int temp = X; temp = temp + 1; X = temp; } void increment() { int temp = X; temp = temp + 1; X = temp; } Thread 1 Thread 2 Answer: A.0 B.1 C.2 D.More than 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Schedules/Interleavings Model of concurrent execution Interleave statements from each thread into a single thread If any interleaving yields incorrect results, some synchronization is needed tmp1 = X; tmp1 = tmp1 + 1; X = tmp1; tmp2 = X; tmp2 = tmp2 + 1; X = tmp2; Thread 1 Thread 2 tmp1 = X; tmp2 = X; tmp2 = tmp2 + 1; tmp1 = tmp1 + 1; X = tmp1; X = tmp2; If X==0 initially, X == 1 at the end. WRONG result!
Background image of page 4
5 Locks fix this with Mutual Exclusion Mutual exclusion ensures only safe interleavings When is mutual exclusion too safe? void increment() { lock.acquire(); int temp = X; temp = temp + 1; X = temp; lock.release(); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Introducing Locks Locks – implement mutual exclusion Two methods Lock::Acquire() – wait until lock is free, then grab it Lock::Release() – release the lock, waking up a waiter, if any With locks, too much milk problem is very easy! Check and update happen as one unit (exclusive access) Lock.Acquire(); if (noMilk) {         buy milk; } Lock.Release(); Lock.Acquire(); if (noMilk) {         buy milk; } Lock.Release(); How can we implement locks? How can we implement locks? Lock.Acquire(); x++; Lock.Release(); Lock.Acquire(); x++; Lock.Release();
Background image of page 6
7 How to think about synchronization code Every thread has the same pattern Entry section: code to attempt entry to critical section Critical section: code that requires isolation (e.g., with mutual exclusion) Exit section: cleanup code after execution of critical region Non-critical section: everything else There can be multiple critical regions in a program Only critical regions that access the same resource (e.g., data structure) need to synchronize with each other while(1) { Entry section Critical section Exit section Non-critical section }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 The correctness conditions Safety Only one thread in the critical region Liveness
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 03/09/2012.

Page1 / 22

06.Locking - MutualExclusion: Primitivesand...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online