State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
Programming Paradigms for Concurrency Lecture 2 - Mutual ...
Transcript of Programming Paradigms for Concurrency Lecture 2 - Mutual ...
![Page 1: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/1.jpg)
Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion
Based on companion slides for The Art of Multiprocessor Programming
by Maurice Herlihy & Nir Shavit
Modified by Thomas Wies
New York University
TexPoint fonts used in EMF.
Read the TexPoint manual before you delete this box.: AAAA
![Page 2: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/2.jpg)
2
Mutual Exclusion
• We will clarify our understanding of
mutual exclusion
• We will also show you how to reason
about various properties in an
asynchronous concurrent setting
![Page 3: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/3.jpg)
Mutual Exclusion
In his 1965 paper E. W. Dijkstra wrote:
"Given in this paper is a solution to a problem which, to
the knowledge of the author, has been an open
question since at least 1962, irrespective of the
solvability. [...] Although the setting of the problem
might seem somewhat academic at first, the author
trusts that anyone familiar with the logical problems
that arise in computer coupling will appreciate the
significance of the fact that this problem indeed can
be solved."
3
![Page 4: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/4.jpg)
4
Mutual Exclusion
• Formal problem definitions
• Solutions for 2 threads
• Solutions for n threads
• Fair solutions
• Inherent costs
![Page 5: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/5.jpg)
5
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 6: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/6.jpg)
6
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 7: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/7.jpg)
7
• “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 8: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/8.jpg)
8
time
• An event a0 of thread A is
– Instantaneous
– No simultaneous events (break ties)
a0
Events
![Page 9: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/9.jpg)
9
time
• A thread A is (formally) a sequence a0,
a1, ... of events
– “Trace” model
– Notation: a0 a1 indicates order
a0
Threads
a1 a2 …
![Page 10: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/10.jpg)
10
• Assign to shared variable
• Assign to local variable
• Invoke method
• Return from method
• Lots of other things …
Example Thread Events
![Page 11: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/11.jpg)
11
Threads are State Machines
Events are
transitions
a0
a1 a2
a3
![Page 12: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/12.jpg)
12
States
• Thread State
– Program counter
– Local variables
• System state
– Object fields (shared variables)
– Union of thread states
![Page 13: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/13.jpg)
13
time
• Thread A
Concurrency
![Page 14: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/14.jpg)
14
time
time
• Thread A
• Thread B
Concurrency
![Page 15: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/15.jpg)
15
time
Interleavings
• Events of two or more threads
– Interleaved
– Not necessarily independent (why?)
![Page 16: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/16.jpg)
16
time
• An interval A0 =(a0,a1) is
– Time between events a0 and a1
a0 a1
Intervals
A0
![Page 17: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/17.jpg)
17
time
Intervals may Overlap
a0 a1 A0
b0 b1 B0
![Page 18: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/18.jpg)
18
time
Intervals may be Disjoint
a0 a1 A0
b0 b1 B0
![Page 19: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/19.jpg)
19
time
Precedence
a0 a1 A0
b0 b1 B0
Interval A0 precedes interval B0
![Page 20: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/20.jpg)
20
Precedence
• Notation: A0 B0
• Formally,
– End event of A0 before start event of B0
– Also called “happens before” or “precedes”
![Page 21: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/21.jpg)
21
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 22: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/22.jpg)
22
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 23: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/23.jpg)
23
Strict Partial Orders (review)
• 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 24: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/24.jpg)
24
Strict Total Orders (review)
• Also
– Irreflexive
– Antisymmetric
– Transitive
• Except that for every distinct A, B,
– Either A B or B A
![Page 25: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/25.jpg)
25
Repeated Events
while (mumble) {
a0; a1;
}
a0k
k-th occurrence of
event a0
A0k
k-th occurrence of
interval A0 =(a0,a1)
![Page 26: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/26.jpg)
26
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 27: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/27.jpg)
27
Locks (Mutual Exclusion)
public interface Lock {
public void lock();
public void unlock();
}
![Page 28: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/28.jpg)
28
Locks (Mutual Exclusion)
public interface Lock {
public void lock();
public void unlock();
}
acquire lock
![Page 29: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/29.jpg)
29
Locks (Mutual Exclusion)
public interface Lock {
public void lock();
public void unlock();
}
release lock
acquire lock
![Page 30: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/30.jpg)
30
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 31: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/31.jpg)
31
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 32: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/32.jpg)
32
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 33: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/33.jpg)
33
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 34: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/34.jpg)
34
Mutual Exclusion
• Let CSik be thread i’s k-th critical
section execution
![Page 35: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/35.jpg)
35
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 36: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/36.jpg)
36
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 37: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/37.jpg)
37
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 38: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/38.jpg)
38
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 39: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/39.jpg)
39
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 40: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/40.jpg)
40
Starvation-Free
• If some thread calls lock()
– It will eventually return
• Individual threads make progress
![Page 41: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/41.jpg)
41
Two-Thread vs n-Thread
Solutions
• 2-thread solutions first
– Illustrate most basic ideas
– Fits on one slide
• Then n-thread solutions
![Page 42: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/42.jpg)
42
class … implements Lock {
…
// thread-local index, 0 or 1
public void lock() {
int i = ThreadID.get();
int j = 1 - i;
…
}
}
Two-Thread Conventions
![Page 43: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/43.jpg)
43
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 44: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/44.jpg)
LockOne
class LockOne implements Lock {
private boolean[] flag = new boolean[2];
public void lock() {
flag[i] = true;
while (flag[j]) {}
}
![Page 45: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/45.jpg)
LockOne
class LockOne implements Lock {
private boolean[] flag = new boolean[2];
public void lock() {
flag[i] = true;
while (flag[j]) {}
} Each thread has flag
![Page 46: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/46.jpg)
LockOne
class LockOne implements Lock {
private boolean[] flag = new boolean[2];
public void lock() {
flag[i] = true;
while (flag[j]) {}
} Set my flag
![Page 47: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/47.jpg)
LockOne
class LockOne implements Lock {
private boolean[] flag = new boolean[2];
public void lock() {
flag[i] = true;
while (flag[j]) {}
}
Wait for other flag to
become false
![Page 48: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/48.jpg)
48
• 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 49: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/49.jpg)
49
• 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 50: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/50.jpg)
50
• readA(flag[B]==false)
writeB(flag[B]=true)
• readB(flag[A]==false)
writeA(flag[A]=true)
From the Assumption
![Page 51: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/51.jpg)
51
• 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 52: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/52.jpg)
52
• 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 53: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/53.jpg)
53
• 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 54: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/54.jpg)
54
• 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 55: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/55.jpg)
55
• 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 56: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/56.jpg)
56
• 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 57: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/57.jpg)
57
Cycle!
![Page 58: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/58.jpg)
58
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 59: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/59.jpg)
59
LockTwo
public class LockTwo implements Lock {
private int victim;
public void lock() {
victim = i;
while (victim == i) {};
}
public void unlock() {}
}
![Page 60: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/60.jpg)
60
LockTwo
public class LockTwo implements Lock {
private int victim;
public void lock() {
victim = i;
while (victim == i) {};
}
public void unlock() {}
}
Let other go
first
![Page 61: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/61.jpg)
61
LockTwo
public class LockTwo implements Lock {
private int victim;
public void lock() {
victim = i;
while (victim == i) {};
}
public void unlock() {}
}
Wait for
permission
![Page 62: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/62.jpg)
62
LockTwo
public class Lock2 implements Lock {
private int victim;
public void lock() {
victim = i;
while (victim == i) {};
}
public void unlock() {}
}
Nothing to do
![Page 63: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/63.jpg)
63
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 64: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/64.jpg)
64
Peterson’s Algorithm
public void lock() {
flag[i] = true;
victim = i;
while (flag[j] && victim == i) {};
}
public void unlock() {
flag[i] = false;
}
![Page 65: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/65.jpg)
65
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 66: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/66.jpg)
66
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 67: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/67.jpg)
67
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 68: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/68.jpg)
68
Peterson’s Algorithm
public void lock() {
flag[i] = true;
victim = i;
while (flag[j] && victim == i) {};
}
public void unlock() {
flag[i] = false;
}
No longer
interested
Announce I’m
interested
Defer to other
Wait while other
interested & I’m
the victim
![Page 69: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/69.jpg)
69
Mutual Exclusion
(1) writeB(Flag[B]=true)writeB(victim=B)
public void lock() {
flag[i] = true;
victim = i;
while (flag[j] && victim == i) {};
}
From the Code
![Page 70: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/70.jpg)
70
Also from the Code
(2) writeA(victim=A)readA(flag[B])
readA(victim)
public void lock() {
flag[i] = true;
victim = i;
while (flag[j] && victim == i) {};
}
![Page 71: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/71.jpg)
71
Assumption
W.L.O.G. assume A is the last
thread to write victim
(3) writeB(victim=B)writeA(victim=A)
![Page 72: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/72.jpg)
72
Combining Observations
(1) writeB(flag[B]=true)writeB(victim=B)
(3) writeB(victim=B)writeA(victim=A)
(2) writeA(victim=A)readA(flag[B])
readA(victim)
![Page 73: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/73.jpg)
73
Combining Observations
(1) writeB(flag[B]=true)writeB(victim=B)
(3) writeB(victim=B)writeA(victim=A)
(2) writeA(victim=A)readA(flag[B])
readA(victim)
![Page 74: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/74.jpg)
74
Combining Observations
(1) writeB(flag[B]=true)writeB(victim=B)
(3) writeB(victim=B)writeA(victim=A)
(2) writeA(victim=A)readA(flag[B])
readA(victim)
A read flag[B] == true and victim == A, so it
could not have entered the CS (QED)
![Page 75: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/75.jpg)
75
Deadlock Free
• Thread blocked – only at while loop
– only if other’s flag is true
– only if it is the victim
• Solo: other’s flag is false
• Both: one or the other not the victim
public void lock() {
…
while (flag[j] && victim == i) {};
![Page 76: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/76.jpg)
76
Starvation 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 77: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/77.jpg)
77
The Filter Algorithm for n
Threads
There are n “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 78: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/78.jpg)
78
Filter
class Filter implements Lock {
int[] level; // level[i] for thread i
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;
}}
…
}
level
victim
n-1
n-1
0
1
0 0 0 0 0 0 4
2
2
Thread 2 at level 4
0
4
![Page 79: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/79.jpg)
79
Filter
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 unlock() {
level[i] = 0;
}}
![Page 80: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/80.jpg)
80
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 81: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/81.jpg)
81
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 82: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/82.jpg)
82
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 83: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/83.jpg)
83
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
Wait as long as someone else is at same or
higher level, and I’m designated victim
![Page 84: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/84.jpg)
84
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 85: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/85.jpg)
85
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 86: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/86.jpg)
86
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-1) L has n-L
assume
prove
![Page 87: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/87.jpg)
87
Proof Structure
ncs
cs
Assumed to enter L-1
By way of contradiction
all enter L
n-L+1 = 4
n-L+1 = 4
A B
Last to
write
victim[L]
Show that A must have seen
L in level[B] and since victim[L] == A
could not have entered
![Page 88: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/88.jpg)
88
Just Like Peterson
(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) {};
}}
From the Code
![Page 89: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/89.jpg)
89
From the Code
(2) writeA(victim[L]=A)readA(level[B])
readA(victim[L])
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 90: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/90.jpg)
90
By Assumption
By assumption, A is the last thread
to write victim[L]
(3) writeB(victim[L]=B)writeA(victim[L]=A)
![Page 91: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/91.jpg)
91
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])
readA(victim[L])
![Page 92: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/92.jpg)
92
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])
readA(victim[L])
![Page 93: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/93.jpg)
93
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])
readA(victim[L])
A read level[B] ≥ L, and victim[L] = A, so it
could not have entered level L!
![Page 94: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/94.jpg)
94
No Starvation
• Filter Lock satisfies properties:
– Just like Peterson Alg at any level
– So no one starves
• But what about fairness?
– Threads can be overtaken by others
![Page 95: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/95.jpg)
95
Bounded Waiting
• Want stronger fairness guarantees
• Thread not “overtaken” too much
• If A starts before B, then A enters
before B?
• But what does “start” mean?
• Need to adjust definitions ….
![Page 96: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/96.jpg)
96
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 97: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/97.jpg)
97
• For threads A and B:
– If DAk DB
j
• A’s k-th doorway precedes B’s j-th doorway
– Then CSAk CSB
j
• A’s k-th critical section precedes B’s j-th critical
section
• B cannot overtake A
First-Come-First-Served
![Page 98: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/98.jpg)
98
Fairness Again
• Filter Lock satisfies properties:
– No one starves
– But very weak fairness
• Can be overtaken arbitrary # of times
– That’s pretty lame…
![Page 99: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/99.jpg)
99
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 100: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/100.jpg)
100
Bakery Algorithm
class Bakery implements Lock {
boolean[] flag;
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;
}
}
…
![Page 101: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/101.jpg)
101
Bakery Algorithm
class Bakery implements Lock {
boolean[] flag;
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-1 0
f f f f t f t
2
f
0 0 0 0 5 0 4 0
6
CS
![Page 102: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/102.jpg)
102
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 103: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/103.jpg)
103
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 104: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/104.jpg)
104
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 105: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/105.jpg)
105
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 106: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/106.jpg)
106
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 107: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/107.jpg)
107
Bakery Algorithm class 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 …
… whose (label[k],k) in
lexicographic order is lower
![Page 108: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/108.jpg)
108
Bakery Algorithm
class Bakery implements Lock {
…
public void unlock() {
flag[i] = false;
}
}
![Page 109: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/109.jpg)
109
Bakery Algorithm
class Bakery implements Lock {
…
public void unlock() {
flag[i] = false;
}
}
No longer
interested
labels are always increasing
![Page 110: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/110.jpg)
110
No Deadlock
• There is always one thread with earliest
label
• Ties are impossible (why?)
![Page 111: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/111.jpg)
111
First-Come-First-Served
• If DA DB then
– A’s label is smaller
• And:
– writeA(label[A])
– readB(label[A])
– writeB(label[B]) readB(flag[A])
• So B sees
– smaller label for A
– 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 112: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/112.jpg)
112
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 113: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/113.jpg)
113
Mutual Exclusion
• Labels are strictly increasing so
• B must have seen flag[A] == false
![Page 114: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/114.jpg)
114
Mutual Exclusion
• Labels are strictly increasing so
• B must have seen flag[A] == false
• LabelingB readB(flag[A])
writeA(flag[A]) LabelingA
![Page 115: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/115.jpg)
115
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 116: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/116.jpg)
116
Bakery Y232K Bug 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 117: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/117.jpg)
117
Bakery Y232K Bug 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));
}
Mutex breaks if label[i] overflows
![Page 118: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/118.jpg)
118
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 119: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/119.jpg)
119
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 120: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/120.jpg)
120
• One can construct a
– Wait-free (no mutual exclusion)
– Concurrent
– Timestamping system
– That never overflows
The Good News
![Page 121: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/121.jpg)
121
• One can construct a
– Wait-free (no mutual exclusion)
– Concurrent
– Timestamping system
– That never overflows
The Good News
This part is hard
![Page 122: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/122.jpg)
122
Instead …
• We construct a Sequential timestamping system
– Same basic idea
– But simpler
• As if we use mutex to read & write atomically
• No good for building locks
– But useful anyway
![Page 123: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/123.jpg)
123
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 124: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/124.jpg)
124
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 125: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/125.jpg)
125
Unbounded Counter Precedence
Graph
0 1 2 3
![Page 126: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/126.jpg)
126
Unbounded Counter Precedence
Graph
0 1 2 3
takes 0 takes 1 takes 2
![Page 127: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/127.jpg)
127
Two-Thread Bounded
Precedence Graph
0
1 2
![Page 128: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/128.jpg)
128
Two-Thread Bounded
Precedence Graph
0
1 2
![Page 129: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/129.jpg)
129
Two-Thread Bounded
Precedence Graph
0
1 2
![Page 130: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/130.jpg)
130
Two-Thread Bounded
Precedence Graph
0
1 2
![Page 131: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/131.jpg)
131
Two-Thread Bounded
Precedence Graph T2
0
1 2
and so on …
![Page 132: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/132.jpg)
132
Three-Thread Bounded
Precedence Graph?
1 2
0 3
![Page 133: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/133.jpg)
133
Three-Thread Bounded
Precedence Graph?
1 2
0 3
What to do if
one thread
gets stuck?
![Page 134: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/134.jpg)
134
Graph Composition
0
1 2
0
1 2
Replace each vertex with a
copy of the graph
T3=T2*T2
![Page 135: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/135.jpg)
135
Three-Thread Bounded
Precedence Graph T3
2
0
1 2 1
0
1 2
0
0
1 2
![Page 136: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/136.jpg)
136
Three-Thread Bounded
Precedence Graph T3
2
0
1 2 1
0
1 2
0
0
1 2
20 02 12 < <
![Page 137: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/137.jpg)
137
Three-Thread Bounded
Precedence Graph T3
2
0
1 2 1
0
1 2
0
0
1 2
and so on…
![Page 138: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/138.jpg)
138
In General
Tk = T2 * Tk-1
K threads need 3k
nodes
label size =
Log2(3k) = 2k
![Page 139: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/139.jpg)
139
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 140: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/140.jpg)
140
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 that interesting: SRMW and SRSW
![Page 141: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/141.jpg)
141
Theorem
At least N MRSW (multi-reader/single-
writer) registers are needed to solve
deadlock-free mutual exclusion.
N registers like Flag[]…
![Page 142: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/142.jpg)
142
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 143: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/143.jpg)
143
Proof: Need N-MRSW Registers
Each thread must write to some register
…can’t tell whether A is in critical section
write
CS CS CS
write
A B C
![Page 144: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/144.jpg)
144
Upper Bound
• Bakery algorithm
– Uses 2N MRSW registers
• So the bound is (pretty) tight
• But what if we use MRMW registers?
– Like victim[] ?
![Page 145: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/145.jpg)
145
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 146: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/146.jpg)
146
Theorem (For 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 147: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/147.jpg)
147
Two Thread Execution
• Threads run, reading and writing R
• Deadlock free so at least one gets in
B A
CS
Write(R)
CS
R
![Page 148: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/148.jpg)
148
Covering State for One Register
Always Exists
Write(R)
B
In any protocol B has to write to the
register before entering CS, so stop
it just before
![Page 149: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/149.jpg)
149
Proof: Assume Cover of 1
A B
Write(R)
CS
A runs, possibly writes to the
register, enters CS
![Page 150: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/150.jpg)
150
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 151: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/151.jpg)
151
Theorem
Deadlock-free mutual exclusion for 3
threads requires at least 3 multi-reader
multi-writer registers
![Page 152: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/152.jpg)
152
Proof: Assume Cover of 2
Write(RB)
B
Write(RC)
C A
Only 2 registers
![Page 153: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/153.jpg)
153
Run A Solo
Write(RB)
B
Write(RC)
C A
Writes to one or both
registers, enters CS CS
![Page 154: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/154.jpg)
154
Obliterate Traces of A
Write(RB)
B
Write(RC)
C A
Other threads obliterate evidence
that A entered CS CS
![Page 155: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/155.jpg)
155
Mutual Exclusion Fails
Write(RB)
B
Write(RC)
C A
CS CS
CS looks empty, so
another thread gets
in
![Page 156: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/156.jpg)
156
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 157: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/157.jpg)
157
• 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 158: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/158.jpg)
158
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 159: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/159.jpg)
159
Covering State for Two
• NO! A could have written to RB
• So CS no longer looks empty to thread C
Write(RB)
B
Write(RA)
A
![Page 160: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/160.jpg)
160
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 161: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/161.jpg)
161
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 162: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/162.jpg)
162
Summary of Lecture
• In the 1960’s several incorrect solutions
to starvation-free mutual exclusion
using RW-registers were published…
• Today we know how to solve FIFO N
thread mutual exclusion using 2N RW-
Registers
![Page 163: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/163.jpg)
163
Summary of Lecture
• N RW-Registers inefficient
– Because writes “cover” older writes
• Need stronger hardware operations
– that do not have the “covering problem”
• In next lectures - understand what these
operations are…
![Page 164: Programming Paradigms for Concurrency Lecture 2 - Mutual ...](https://reader030.fdocuments.us/reader030/viewer/2022012722/61b42709d27e0c2b2b2b741d/html5/thumbnails/164.jpg)
164
This work is licensed under a Creative Commons Attribution-
ShareAlike 2.5 License.
• You are free:
– to Share — to copy, distribute and transmit the work
– to Remix — to adapt the work
• Under the following conditions:
– Attribution. You must attribute the work to “The Art of Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).
– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to
– http://creativecommons.org/licenses/by-sa/3.0/.
• Any of the above conditions can be waived if you get permission from the copyright holder.
• Nothing in this license impairs or restricts the author's moral rights.