mutual exclusion the art of multiprocessor programming spring 2007

170
Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

Upload: antony-bentley

Post on 14-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

Mutual Exclusion

The Art of Multiprocessor Programming

Spring 2007

Page 2: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 2

Review

• Model of computation• Asynchrony• Mutual exclusion

Page 3: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 3

Sequential Computation

memory

object object

thread

Page 4: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 4

Concurrent Computation

memory

object object

thre

ads

Page 5: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 5

Asynchrony

• Sudden unpredictable delays– Cache misses (short)– Page faults (long)– Scheduling quantum used up (really

long)

Page 6: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 6

Model Summary

• Multiple threads– Sometimes called processes

• Multiple CPU’s– Sometimes called processors

• Single shared memory• Objects live in memory• Unpredictable asynchronous delays

Page 7: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 7

Parallel Primality Testing

• Split the work evenly• Each thread tests range of 109

…109 10102·1091

P0 P1 P9

Page 8: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 8

Procedure for Thread i

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); }}

Page 9: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 9

17

18

19

Shared Counter

each thread takes a number

Page 10: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 10

Procedure for Thread i

int counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Page 11: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 11

Where Things Reside

cache

BusBus

cachecache

1

shared counter

shared memory

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); }}

code

Local variables

Page 12: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 12

Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Procedure for Thread i

Increment & return each new

value

Page 13: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 13

Counter Implementation

public class Counter { private long value;

public long getAndIncrement() { return value++; }}

Page 14: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 14

What It Means

public class Counter { private long value;

public long getAndIncrement() { return value++; }}

temp = value; value = value + 1; return temp;

Page 15: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 15

Challenge

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; }}

Make these steps atomic (indivisible)

Page 16: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 16

Mutual Exclusion

• Today we will try to formalize our understanding of mutual exclusion

• We will also use the opportunity to show you how to argue about and prove various properties in an asynchronous concurrent setting

Page 17: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 17

Mutual Exclusion

• Formal problem definitions• Solutions for 2 threads• Solutions for n threads• Fair solutions• Inherent costs

Page 18: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 18

Warning

• You will never use these protocols– Get over it

• You are advised to understand them– The same issues show up everywhere– Except hidden and more complex

Page 19: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 19

Why is Concurrent Programming so Hard?

• Try preparing a seven-course banquet– By yourself– With one friend– With twenty-seven friends …

• Before we can talk about programs– Need a language– Describing time and concurrency

Page 20: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 20

• “Absolute, true and mathematical time, of itself and from its own nature, flows equably without relation to anything external.” (I. Newton, 1689)

• “Time is, like, Nature’s way of making sure that everything doesn’t happen all at once.” (Anonymous, circa 1968)

Time

time

Page 21: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 21

time

• An event a0 of thread A is– Instantaneous– No simultaneous events (break ties)

a0

Events

Page 22: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 22

time

• A thread A is (formally) a sequence a0, a1, ... of events – “Trace” model

– Notation: a0 a1 indicates order

a0

Threads

a1 a2 …

Page 23: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 23

• Assign to shared variable• Assign to local variable• Invoke method• Return from method• Lots of other things …

Example Thread Events

Page 24: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 24

Threads are State Machines

Events are transitions

a0

a1a2

a3

Page 25: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 25

States

• Thread State– Program counter– Local variables

• System state– Object fields (shared variables)– Union of thread states

Page 26: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 26

time

• Thread A

Concurrency

Page 27: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 27

time

time

• Thread A

• Thread B

Concurrency

Page 28: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 28

time

Interleavings

• Events of two or more threads– Interleaved– Not necessarily independent (why?)

Page 29: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 29

time

• An interval A0 =(a0,a1) is

– Time between events a0 and a1

a0 a1

Intervals

A0

Page 30: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 30

time

Intervals may Overlap

a0 a1A0

b0 b1B0

Page 31: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 31

time

Intervals may be Disjoint

a0 a1A0

b0 b1B0

Page 32: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 32

time

Precedence

a0 a1A0

b0 b1B0

Interval A0 precedes interval B0

Page 33: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 33

Precedence

• Notation: A0 B0

• Formally,– End event of A0 before start event of B0

– Also called “happens before” or “precedes”

Page 34: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 34

Precedence Ordering

• Remark: A0 B0 is just like saying – 1066 AD 1492 AD, – Middle Ages Renaissance,

• Oh wait, – what about this week vs this month?

Page 35: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 35

Precedence Ordering

• Never true that A A

• If A B then not true that B A

• If A B & B C then A C

• Funny thing: A B & B A might both be false!

Page 36: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 36

Partial Orders(you may know this already)

• Irreflexive:– Never true that A A

• Antisymmetric:– If A B then not true that B A

• Transitive:– If A B & B C then A C

Page 37: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 37

Total Orders(you may know this already)

• Also– Irreflexive– Antisymmetric– Transitive

• Except that for every distinct A, B,– Either A B or B A

Page 38: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 38

Repeated Events

while (mumble) { a0; a1;}

a0k

k-th occurrence of event a0

A0k

k-th occurrence of interval A0 =(a0,a1)

Page 39: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 39

Implementing a Counter

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; }}

Make these steps indivisible using

locks

Page 40: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 40

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();}

Page 41: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 41

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();}

acquire lock

Page 42: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 42

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();}

release lock

acquire lock

Page 43: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 43

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Page 44: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 44

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Acquire Lock

Page 45: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 45

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Release lock(no matter what)

Page 46: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 46

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Critical section

Page 47: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 47

Using Locks

private Lock lock;…while{mumble}{ <non critical section> lock.lock(); <critical section> lock.unlock(); }…

Only Well formed ExecutionsWhat are the required properties of locks?

Page 48: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 48

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

Page 49: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 49

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be thread j’s m-th critical

section execution

Page 50: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 50

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either– or

Page 51: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 51

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either– or

CSik CSj

m

Page 52: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 52

Mutual Exclusion

• Let CSik be thread i’s k-th critical

section execution

• And CSjm be j’s m-th execution

• Then either– or

CSik CSj

m

CSjm CSi

k

Page 53: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 53

Deadlock-Free

• If some thread calls lock()– And never returns– Then other threads must complete

lock() and unlock() calls infinitely often

• System as a whole makes progress– Even if individuals starve

Page 54: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 54

Lockout-Free

• If some thread calls lock()– It will eventually return

• Individual threads make progress

Page 55: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 55

Two-Thread vs n -Thread Solutions

• Two-thread solutions first– Illustrate most basic ideas– Fits on one slide

• Then n-Thread solutions

Page 56: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 56

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; … }}

Two-Thread Conventions

Page 57: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 57

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; … } }

Two-Thread Conventions

Henceforth: i is current thread, j is other thread

Page 58: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 58

LockOneclass LockOne implements Lock {private volatile boolean[] flag = new boolean[2];public void lock() { flag[i] = true; while (flag[j]) {} }

Page 59: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 59

LockOneclass LockOne implements Lock {private volatile boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }

Set my flag

Page 60: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 60

class LockOne implements Lock {private volatile boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {} }

LockOne

Set my flag

Wait for other flag to go false

Page 61: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 61

• Assume CSAj overlaps CSB

k

• Consider each thread's last (j-th and k-th) read and write in the lock() method before entering

• Derive a contradiction

LockOne Satisfies Mutual Exclusion

Page 62: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 62

• writeA(flag[A]=true) readA(flag[B]==false) CSA

• writeB(flag[B]=true) readB(flag[A]==false) CSB

From the Code

class LockOne implements Lock {… public void lock() { flag[i] = true; while (flag[j]) {} }

Page 63: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 63

• readA(flag[B]==false) writeB(flag[B]=true)

• readB(flag[A]==false) writeA(flag[B]=true)

From the Assumption

Page 64: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 64

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 65: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 65

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 66: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 66

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 67: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 67

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 68: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 68

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 69: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 69

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 70: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 70

Cycle!

Page 71: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 71

Deadlock Freedom

• LockOne Fails deadlock-freedom– Concurrent execution can deadlock

– Sequential executions OK

flag[i] = true; flag[j] = true; while (flag[j]){} while (flag[i]){}

Page 72: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 72

LockTwopublic class LockTwo implements Lock { private volatile int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Page 73: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 73

LockTwopublic class LockTwo implements Lock { private volatile int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Let other go first

Page 74: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 74

LockTwopublic class LockTwo implements Lock { private volatile int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Wait for permission

Page 75: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 75

LockTwopublic class Lock2 implements Lock { private volatile int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Nothing to do

Page 76: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 76

public void LockTwo() { victim = i; while (victim == i) {}; }

LockTwo Claims

• Satisfies mutual exclusion– If thread i in CS– Then victim == j– Cannot be both 0 and 1

• Not deadlock free– Sequential execution deadlocks– Concurrent execution does not

Page 77: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 77

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Page 78: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 78

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I’m interested

Page 79: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 79

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I’m interested

Defer to other

Page 80: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 80

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I’m interested

Defer to other

Wait while other interested & I’m

the victim

Page 81: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 81

Peterson’s Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; }public void unlock() { flag[i] = false;}

Announce I’m interested

Defer to other

Wait while other interested & I’m

the victimNo longer interested

Page 82: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 82

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};

Mutual Exclusion

• If thread 1 in critical section,– flag[1]=true, – victim = 0

• If thread 0 in critical section,– flag[0]=true, – victim = 1

Cannot both be true

Page 83: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 83

Deadlock Free

• Thread blocked – only at while loop– only if it is the victim

• One or the other must not be the victim

public void lock() { … while (flag[j] && victim == i) {};

Page 84: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 84

Lockout Free

• Thread i blocked only if j repeatedly re-enters so that

flag[j] == true and victim == i

• When j re-enters– it sets victim to j.– So i gets in

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}

public void unlock() { flag[i] = false; }

Page 85: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 85

The Filter Algorithm for n Threads

There are n-1 “waiting rooms” called levels

• At each level – At least one enters level– At least one blocked if many try

• Only one thread makes it through

ncs

cs

Page 86: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 86

Filterclass Filter implements Lock { volatile int[] level; // level[i] for thread i volatile int[] victim; // victim[L] for level L

public Filter(int n) { level = new int[n]; victim = new int[n]; for (int i = 1; i < n; i++) { level[i] = 0; }}…

}

n-1

n-1

0

1

0 0 0 0 0 04

2

2

Thread 2 at level 4

0

4

level

victim

Page 87: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 87

Filterclass Filter implements Lock { …

public void lock(){ for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i level[k] >= L) && victim[L] == i ); }} public void unlock() { level[i] = 0; }}

Page 88: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 88

class Filter implements Lock { … public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

One level at a time

Page 89: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 89

class Filter implements Lock { … public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i); // busy wait }} public void release(int i) { level[i] = 0; }}

Filter

Announce intention to enter level L

Page 90: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 90

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

Give priority to anyone but me

Page 91: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 91

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

FilterWait as long as someone else is at

same or higher level, and I’m designated victim

Page 92: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 92

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

Thread enters level L when it completes the loop

Page 93: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 93

Claim• Start at level L=0• At most n-L threads enter level L• Mutual exclusion at level L=n-1

ncs

cs L=n-1

L=1

L=n-2

L=0

Page 94: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 94

Induction Hypothesis

• Assume all at level L-1 enter level L

• A last to write victim[L]

• B is any other thread at level L

• No more than n-L+1 at level L-1 • Induction step: by contradiction

ncs

cs

L-1 has n-L+1L has n-L

assume

prove

Page 95: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 95

Proof Structurencs

cs

Assumed to enter L-1

By way of contradictionall enter L

n-L+1 = 4

n-L+1 = 4

A B

Last to writevictim[L]

Show that A must have seen B at level L and since victim[L] == Acould not have entered

Page 96: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 96

From the Code

(1) writeB(level[B]=L)writeB(victim[L]=B)

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i) {}; }}

Page 97: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 97

From the Code

(2) writeA(victim[L]=A)readA(level[B])

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((k != i) level[k] >= L) && victim[L] == i) {}; }}

Page 98: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 98

By Assumption

By assumption, A is the last thread to write victim[L]

(3) writeB(victim[L]=B)writeA(victim[L]=A)

Page 99: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 99

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B)

(3) writeB(victim[L]=B)writeA(victim[L]=A)

(2) writeA(victim[L]=A)readA(level[B])

Page 100: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 100

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i; while ((k != i) level[k] >= L)

&& victim[L] == i) {}; }}

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B)

(3) writeB(victim[L]=B)writeA(victim[L]=A)

(2) writeA(victim[L]=A)readA(level[B])

Page 101: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 101

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B)

(3) writeB(victim[L]=B)writeA(victim[L]=A)

(2) writeA(victim[L]=A)readA(level[B])

Thus, A read level[B] ≥ L, A was last to write victim[L],so it could not have entered level L!

Page 102: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 102

No Lockout

• Filter Lock satisfies properties:– Just like Peterson Alg at any level– So no one starves (no lockout)

• But what about fairness?– Threads can be overtaken by others

Page 103: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 103

Bounded Waiting

• Want stronger fairness guarantees• Thread not “overtaken” too much• Need to adjust definitions ….

Page 104: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 104

Bounded Waiting

• Divide lock() method into 2 parts:– Doorway interval:

• Written DA

• always finishes in finite steps

– Waiting interval:• Written WA

• may take unbounded steps

Page 105: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 105

• For threads A and B:– If DA

k DB j

• A’s k-th doorway precedes B’s j-th doorway

– Then CSAk CSB

j+r

• A’s k-th critical section precedes B’s (j+r)-th critical section

• B cannot overtake A by more than r times

• First-come-first-served means r = 0.

r-Bounded Waiting

Page 106: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 106

Fairness Again

• Filter Lock satisfies properties:– No one starves (no lockout)– But very weak fairness

•Not r-bounded for any r!– That’s pretty lame…

Page 107: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 107

Bakery Algorithm

• Provides First-Come-First-Served• How?

– Take a “number”– Wait until lower numbers have been

served• Lexicographic order

– (a,i) > (b,j)• If a > b, or a = b and i > j

Page 108: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 108

Bakery Algorithmclass Bakery implements Lock { volatile boolean[] flag; volatile Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …

n-10

f f f f t ft

2

f

0 0 0 0 5 04 0

6

CS

Page 109: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 109

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Page 110: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 110

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Doorway

Page 111: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 111

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

I’m interested

Page 112: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 112

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Take increasing label (read labels in some arbitrary

order)

Page 113: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 113

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Someone is interested

Page 114: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 114

Bakery Algorithmclass Bakery implements Lock { boolean flag[n]; int label[n];

public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Someone is interested

With lower (label,i) in lexicographic order

Page 115: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 115

Bakery Algorithm

class Bakery implements Lock { …

public void unlock() { flag[i] = false; }}

Page 116: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 116

Bakery Algorithm

class Bakery implements Lock { …

public void unlock() { flag[i] = false; }}

No longer interested

Notice that this means that labels are forever monotonically increasing

Page 117: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 117

No Deadlock

• There is always one thread with earliest label

• Ties are impossible (why?)

Page 118: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 118

First-Come-First-Served

• If DA DBthen A’s label is earlier– writeA(label[A])

readB(label[A]) writeB(label[B]) readB(flag[A])

• So B is locked out while flag[A] is true

class Bakery implements Lock {

public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1; while (k flag[k] && (label[i],i) > (label[k],k));

}

Page 119: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 119

Mutual Exclusion

• Suppose A and B in CS together

• Suppose A has earlier label

• When B entered, it must have seen– flag[A] is false, or– label[A] > label[B]

class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1; while (k flag[k] && (label[i],i) > (label[k],k));

}

Page 120: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 120

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false

Page 121: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 121

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false

• LabelingB readB(flag[A]) writeA(flag[A]) LabelingA

Page 122: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 122

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false

• LabelingB readB(flag[A]) writeA(flag[A]) LabelingA

• Which contradicts the assumption that A has an earlier label

Page 123: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 123

Bakery Y232K Bugclass Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Page 124: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 124

Bakery Y232K Bugclass Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while (k flag[k] && (label[i],i) > (label[k],k)); }

Mutex breaks if label[i] overflows

Page 125: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 125

Does Overflow Actually Matter?

• Yes– Y2K– 18 January 2038 (Unix time_t rollover)– 16-bit counters

• No– 64-bit counters

• Maybe– 32-bit counters

Page 126: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 126

Timestamps

• Label variable is really a timestamp

• Need ability to– Read others’ timestamps– Compare them– Generate a later timestamp

• Can we do this without overflow?

Page 127: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 127

• One can construct a– Wait-free (no mutual exclusion)– Concurrent– Timestamping system– That never overflows

The Good News

Page 128: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 128

• One can construct a– Wait-free (no mutual exclusion)– Concurrent– Timestamping system– That never overflows

The Good News

This part is hard

Bad

Page 129: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 129

Instead …

• We construct a Sequential timestamping system– Same basic idea– But simpler

• Uses mutex to read & write atomically

• No good for building locks– But useful anyway

Page 130: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 130

Precedence Graphs

0 1 2 3

• Timestamps form directed graph• Edge x to y

– Means x is later timestamp– We say x dominates y

Page 131: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 131

Unbounded Counter Precedence Graph

0 1 2 3

• Timestamping = move tokens on graph• Atomically

– read others’ tokens – move mine

• Ignore tie-breaking for now

Page 132: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 132

Unbounded Counter Precedence Graph

0 1 2 3

Page 133: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 133

Unbounded Counter Precedence Graph

0 1 2 3

takes 0 takes 1 takes 2

Page 134: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 134

Two-Thread Bounded Precedence Graph

0

12

Page 135: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 135

Two-Thread Bounded Precedence Graph

0

12

Page 136: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 136

Two-Thread Bounded Precedence Graph

0

12

Page 137: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 137

Two-Thread Bounded Precedence Graph

0

12

Page 138: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 138

Two-Thread Bounded Precedence Graph T2

0

12

and so on …

Page 139: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 139

Three-Thread Bounded Precedence Graph?

12

03

Page 140: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 140

Three-Thread Bounded Precedence Graph?

12

03Not clear

what to do if one thread gets stuck

Page 141: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 141

Graph Composition

0

12

0

12

Replace each vertex with a copy of the graph

T3=T2*T2

Page 142: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 142

Three-Thread Bounded Precedence Graph T3

20

1210

12

00

12

Page 143: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 143

Three-Thread Bounded Precedence Graph T3

20

1210

12

00

12

20 02 12<<

Page 144: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 144

Three-Thread Bounded Precedence Graph T3

20

121

0

12

00

12

and so on…

Page 145: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 145

In General

Tk = T2 * Tk-1

K threads need 3k nodes

label size = Log2(3k) = 2n

Page 146: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 146

Deep Philosophical Question

• The Bakery Algorithm is– Succinct,– Elegant, and– Fair.

• Q: So why isn’t it practical?• A: Well, you have to read N distinct

variables

Page 147: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 147

Shared Memory

• Shared read/write memory locations called Registers (historical reasons)

• Come in different flavors– Multi-Reader-Single-Writer (Flag[])– Multi-Reader-Multi-Writer (Victim[])– Not interesting: SRMW and SRSW

Page 148: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 148

Theorem

At least N MRSW (multi-reader/single-writer) registers are needed to solve deadlock-free mutual exclusion.

N registers like Flag[]…

Page 149: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 149

Proving Algorithmic Impossibility

CS

write

C

•To show no algorithm exists:• assume by way of contradiction one does, • show a bad execution that violates properties: • in our case assume an alg for deadlock free mutual exclusion using <n registers

Page 150: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 150

Proof: Need N-MRSW RegistersEach thread must write to some register

…can’t tell whether A is in critical section

write

CS CS CS

write

A B C

Page 151: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 151

Upper Bound

• Bakery algorithm– Uses 2N MRSW registers

• So the bound is (pretty) tight• But what if we use MRMW

registers?– Like victim[] ?

Page 152: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 152

Bad News Theorem

At least N MRMW multi-reader/multi-writer registers are needed to solve deadlock-free mutual exclusion.

(So multiple writers don’t help)

Page 153: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 153

Theorem (First 2-Threads)

Theorem: Deadlock-free mutual exclusion for 2 threads requires at least 2 multi-reader multi-writer registers

Proof: assume one register suffices and derive a contradiction

Page 154: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 154

Two Thread Execution

• Threads run, reading and writing R• Deadlock free so at least one gets in

BA

CS

Write(R)

CS

R

Page 155: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 155

Covering State for One Register

Write(R)

B

B has to write to the register before entering CS, so stop it

just before

Page 156: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 156

Proof: Assume Cover of 1

A B

Write(R)

CS

A runs, possibly writes to the register, enters CS

Page 157: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 157

Proof: Assume Cover of 1

A B

CS

B Runs, first obliterating any trace of A, then also enters the critical section

Write(R)

CS

Page 158: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 158

Theorem

Deadlock-free mutual exclusion for 3 threads requires at least 3 multi-reader multi-writer registers

Page 159: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 159

Proof: Assume Cover of 2

Write(RB)

B

Write(RC)

CA

Only 2 registers

Page 160: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 160

Run A Solo

Write(RB)

B

Write(RC)

CA

Writes to one or both registers, enters CS CS

Page 161: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 161

Obliterate Traces of A

Write(RB)

B

Write(RC)

CA

Other threads obliterate evidence that A entered CS CS

Page 162: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 162

Mutual Exclusion Fails

Write(RB)

B

Write(RC)

CA

CS CS

CS looks empty, so another thread

gets in

Page 163: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 163

Proof Strategy

• Proved: a contradiction starting from a covering state for 2 registers

• Claim: a covering state for 2 registers is reachable from any state where CS is empty

Page 164: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 164

• If we run B through CS 3 times, B must return twice to cover some register, say RB

Covering State for Two

Write(RB)

B

Write(RA)

A

Page 165: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 165

Covering State for Two

• Start with B covering register RB for the 1st time • Run A until it is about to write to uncovered RA

• Are we done?

Write(RB)

B

Write(RA)

A

Page 166: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 166

Covering State for Two

• NO! A could have written to RB

• So CS no longer looks empty

Write(RB)

B

Write(RA)

A

Page 167: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 167

Covering State for Two

• Run B obliterating traces of A in RB

• Run B again until it is about to write to RB

• Now we are done

Write(RB)

B

Write(RA)

A

Page 168: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 168

Inductively We Can Show

• There is a covering state– Where k threads not in CS cover k distinct

registers– Proof follows when k = N-1

Write(RB)

B

Write(RC)

C

Write(RA)

A

Page 169: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 169

Summary of Lecture

• In the 1960’s many incorrect solutions to lockout-free mutual exclusion using RW-registers were published…

• Today we know how to solve FIFO N thread mutual exclusion using 2N RW-Registers

Page 170: Mutual Exclusion The Art of Multiprocessor Programming Spring 2007

© 2006 Herlihy and Shavit 170

Summary of Lecture

• N RW-Registers inefficient– Because writes “cover” older writes

• Need stronger hardware operations – do not have the “covering problem”

• In next lectures - understand what these operations are…