Concurrency - Computer Science and...
Transcript of Concurrency - Computer Science and...
![Page 1: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/1.jpg)
1
![Page 2: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/2.jpg)
T1:
data = ...;flag = true;
T2:
if (flag)
... = data;
Initially:int data = 0;
boolean flag = false;
2
![Page 3: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/3.jpg)
3
![Page 4: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/4.jpg)
“From my perspective, parallelism is the biggest challenge since high‐level programming languages. It’s the biggest thing in 50 years because industry is betting its future that parallel programming will be useful.
“Industry is building parallel hardware, assuming people can use it. And I think there's a chance they'll fail since the software is not necessarily in place. So this is a gigantic challenge facing the computer science community. If we miss this opportunity, it's going to be bad for the industry.”
—David Patterson, ACM Queue interview, 2006
4
![Page 5: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/5.jpg)
Imperative programs Java, C#, C, C++, Python, Ruby
Threads Shared, mutable state Synchronization primitives:▪ Lock acquire & release▪ Monitor wait & notify▪ Thread start & join
5
![Page 6: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/6.jpg)
More synchronizationLess synchronization
Concurrency bugs: atomicity, order, &sequential consistency violations
More concurrency
Concurrency bug: deadlock
Poor performance: lock contention, serialization
Concurrent & correct
Less concurrency
6
![Page 7: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/7.jpg)
C++11 memory model [Boehm & Adve ’08] Java memory model [Manson et al. ’05]
7
![Page 8: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/8.jpg)
Region serializability of synchronization‐free regions
Sequentially consistent
Program executions have different behaviors
![Page 9: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/9.jpg)
Region serializability of synchronization‐free regions
Sequentially consistent
DRF0: strong guarantees for race-free programs
![Page 10: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/10.jpg)
Region serializability of synchronization‐free regions
Sequentially consistent
DRF0: no guarantees for racy programs
![Page 11: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/11.jpg)
Region serializability of synchronization‐free regions
Sequentially consistent
DRF0: no guarantees for racy programs
Data races are here to stayDynamic data race detection [Flanagan & Freund ’09]Static data race detection [Naik & Aiken ’07]Data‐race‐free language design [Boyapati et al. ’02]
![Page 12: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/12.jpg)
C++11 memory model [Boehm & Adve ’08] Data races no semantics
Java memory model [Manson et al. ’05] Data races only type & memory safety preserved
But… the JMM is actually broken
12
![Page 13: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/13.jpg)
Two accesses to same variable At least one is a write
Not well‐synchronized(not ordered by happens‐before relationship)
Or: accesses can happen simultaneously
13
![Page 14: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/14.jpg)
T1:
data = ...;flag = true;
T2:
if (flag)
... = data;
Initially:int data = 0;
boolean flag = false;
14
![Page 15: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/15.jpg)
T1:
data = ...;
flag = true;
T2:
if (flag)
... = data;
Initially:int data = 0;
boolean flag = false;
15
![Page 16: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/16.jpg)
T1:
data = ...;flag = true;
T2:
if (flag)
... = data;
Initially:int data = 0;
boolean flag = false;
16
![Page 17: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/17.jpg)
T1:
flag = true;
data = ...;
T2:
if (flag)
... = data;
Initially:int data = 0;
boolean flag = false;
17
![Page 18: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/18.jpg)
T1:
data = ...;flag = true;
T2:
tmp = data;
if (flag)
... = tmp;
Initially:int data = 0;
boolean flag = false;
18
![Page 19: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/19.jpg)
T1:
data = ...;synchronized (m) {flag = true;
}
T2:
boolean tmp;
synchronized (m) {
tmp = flag;
}
if (tmp)
... = data;
Initially:int data = 0;
boolean flag = false;
19
![Page 20: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/20.jpg)
T1:
data = ...;acquire(m);flag = true;
release(m);
T2:
boolean tmp;
acquire(m);
tmp = flag;
release(m);
if (tmp)
... = data;
Initially:int data = 0;
boolean flag = false;
20
![Page 21: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/21.jpg)
T1:
data = ...;acquire(m);flag = true;
release(m);
T2:
boolean tmp;
acquire(m);
tmp = flag;
release(m);
if (tmp)
... = data;
Initially:int data = 0;
boolean flag = false;
21
Compiler and hardware obey rules about reordering across synchronization
![Page 22: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/22.jpg)
T1:
data = ...;flag = true;
T2:
if (flag)
... = data;
Initially:int data = 0;
volatile boolean flag = false;
22
![Page 23: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/23.jpg)
T1:
data = 42;flag = true;
T2:
while (!flag) { }
print(data);
Initially:int data = 0;
boolean flag = false;
23
![Page 24: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/24.jpg)
T1:
data = 42;flag = true;
T2:
boolean f;
do {
f = flag;
} while (!f);
int d = data;
print(d);
Initially:int data = 0;
boolean flag = false;
24
![Page 25: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/25.jpg)
T1:
flag = true;
data = 42;
T2:
boolean f;
do {
f = flag;
} while (!f);
int d = data;
print(d);
Initially:int data = 0;
boolean flag = false;
25
![Page 26: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/26.jpg)
T1:
data = 42;flag = true;
T2:
int d = data;
boolean f;
do {
f = flag;
} while (!f);
print(d);
Initially:int data = 0;
boolean flag = false;
26
![Page 27: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/27.jpg)
class Movie {Vector<String> comments;
addComment(String s) {if (comments == null) {
comments = new Vector<String>();}comments.add(s);
}}
27
![Page 28: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/28.jpg)
class Movie {Vector<String> comments;
addComment(String s) {synchronized (this) {if (comments == null) {comments = new Vector<String>();}
}comments.add(s);
}}
28
![Page 29: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/29.jpg)
class Movie {Vector<String> comments;
addComment(String s) {if (comments == null) {synchronized (this) {if (comments == null) {comments = new Vector<String>();
}}
}comments.add(s);
}}
29
![Page 30: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/30.jpg)
addComment(String s) {if (comments == null) {synchronized (this) {if (comments == null) {comments =new Vector<String>();
}}
}
comments.add(s);}
addComment(String s) {
if (comments == null) {
}
comments.add(s);
}
30
![Page 31: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/31.jpg)
addComment(String s) {if (comments == null) {synchronized (this) {if (comments == null) {Vector temp =alloc Vector;temp.<init>();comments = temp;}}
}
comments.add(s);}
addComment(String s) {
if (comments == null) {
}
comments.add(s);
}
31
![Page 32: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/32.jpg)
addComment(String s) {if (comments == null) {synchronized (this) {if (comments == null) {Vector temp =alloc Vector;temp.<init>();comments = temp;}}
}
comments.add(s);}
addComment(String s) {
if (comments == null) {
}
comments.add(s);
}
32
![Page 33: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/33.jpg)
addComment(String s) {if (comments == null) {synchronized (this) {if (comments == null) {Vector temp =alloc Vector;comments = temp;
temp.<init>();}}
}comments.add(s);}
addComment(String s) {
if (comments == null) {
}
comments.add(s);
}
33
![Page 34: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/34.jpg)
C++11 memory model [Boehm & Adve ’08] Data races no semantics
Java memory model [Manson et al. ’05] Data races only type & memory safety preserved But… the JMM is actually broken
“Nondeterminism is unavoidable, but data races are pure evil” [Boehm ’12]“Memory Models: A Case for Rethinking Parallel Languages and Hardware” [Adve & Boehm ’10]“The Case for System Support for Concurrency Exceptions” [Ceze et al. ’09]
34
![Page 35: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/35.jpg)
Region serializability of synchronization‐free regions
Sequentially consistent
RS of synchronization-free regions [Ouyang et al. ’13]Both racy and non-racy executions provide RS
![Page 36: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/36.jpg)
Operations appear to happen all at once or not at all
Serializability – execution equivalent to some serial execution of atomic blocks
36
![Page 37: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/37.jpg)
T1:
t = x;t = t + 3;x = t;
T2:
t = x;t = t * 2;x = t;
int x = 1;
37
![Page 38: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/38.jpg)
T1:
t = x;
t = t + 3;
x = t;
T2:
t = x;
t = t * 2;
x = t;
int x = 1;
38
![Page 39: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/39.jpg)
T1:synchronized(m) {t = x;
}
t = t + 3;
synchronized(m) {x = t;
}
T2:
synchronized(m) {t = x;
}
t = t * 2;
synchronized(m) {x = t;
}
int x = 1;
39
![Page 40: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/40.jpg)
T1:
synchronized (m) {t = x;t = t + 3;x = t;
}
T2:
synchronized (m) {t = x;t = t * 2;x = t;
}
int x = 1;
40
![Page 41: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/41.jpg)
T1:
synchronized (m) {t = x;t = t + 3;x = t;
}
T2:
synchronized (m) {t = x;t = t * 2;x = t;
}
int x = 1;
41
![Page 42: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/42.jpg)
T1:
synchronized (m) {t = x;t = t + 3;x = t;
}
T2:
synchronized (m) {t = x;t = t * 2;x = t;
}
int x = 1;
42
![Page 43: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/43.jpg)
class Vector {synchronized boolean contains(Object o) { ... }synchronized void add(Object o) { ... }
}
43
![Page 44: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/44.jpg)
class Vector {synchronized boolean contains(Object o) { ... }synchronized void add(Object o) { ... }
}
class Set {Vector vector;void add(Object o) {if (!vector.contains(o)) {vector.add(o);
}}
}
44
![Page 45: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/45.jpg)
class Vector {synchronized boolean contains(Object o) { ... }synchronized void add(Object o) { ... }
}
class Set {Vector vector;synchronized void add(Object o) {if (!vector.contains(o)) {vector.add(o);
}}
}
45
![Page 46: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/46.jpg)
class Vector {synchronized boolean contains(Object o) { ... }synchronized void add(Object o) { ... }
}
class Set {Vector vector;void add(Object o) {atomic {if (!vector.contains(o)) {vector.add(o);
}}
}}
46
![Page 47: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/47.jpg)
Java provides memory & type safety Buffer overflows, dangling pointers, array out‐of‐bounds, double frees, some memory leaks How are these handled? With exceptions?
47
![Page 48: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/48.jpg)
Java provides memory & type safety Buffer overflows, dangling pointers, array out‐of‐bounds, double frees, some memory leaks How are these handled? With exceptions?
Should languages (and the runtime systems & hardware that support them) provide
concurrency correctness?
Check & enforce: atomicity, strong memory model, determinism
48
![Page 49: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/49.jpg)
General‐purpose parallel software: hard & unsolved
Challenging semantics for parallel programs
Understand how to write correct, scalable programs one of few experts
49
![Page 50: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/50.jpg)
![Page 51: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/51.jpg)
Two accesses to same variable (one is a write)
One access doesn’t happen before the other Program order Synchronization order▪ Acquire‐release▪ Wait‐notify▪ Fork‐join▪ Volatile read‐write
![Page 52: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/52.jpg)
Thread A
write x
unlock m
Thread B Two accesses to same variable (one is a write)
One access doesn’t happen before the other Program order Synchronization order▪ Acquire‐release▪ Wait‐notify▪ Fork‐join▪ Volatile read‐write
![Page 53: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/53.jpg)
Thread A
write x
unlock m
Thread B
lock m
write x
Two accesses to same variable (one is a write)
One access doesn’t happen before the other Program order Synchronization order▪ Acquire‐release▪ Wait‐notify▪ Fork‐join▪ Volatile read‐write
![Page 54: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/54.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
Two accesses to same variable (one is a write)
One access doesn’t happen before the other Program order Synchronization order▪ Acquire‐release▪ Wait‐notify▪ Fork‐join▪ Volatile read‐write
![Page 55: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/55.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
Two accesses to same variable (one is a write)
One access doesn’t happen before the other Program order Synchronization order▪ Acquire‐release▪ Wait‐notify▪ Fork‐join▪ Volatile read‐write
![Page 56: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/56.jpg)
How do dynamic data race detectors work?
Lockset: check a locking discipline Vector clocks: check happens‐before
![Page 57: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/57.jpg)
Tracks happens‐before: sound & precise 80X slowdown Each analysis step: O(n) time (n = # of threads)
![Page 58: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/58.jpg)
Tracks happens‐before: sound & precise 80X slowdown Each analysis step: O(n) time (n = # of threads)
FastTrack [Flanagan & Freund ’09]
Reads & writes (97%): O(1) time Synchronization (3%): O(n) time 8X slowdown
![Page 59: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/59.jpg)
Tracks happens‐before: sound & precise 80X slowdown Each analysis step: O(n) time (n = # of threads)
FastTrack [Flanagan & Freund ’09]
Reads & writes (97%): O(1) time Synchronization (3%): O(n) time 8X slowdown
Problem today
Problem in future
![Page 60: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/60.jpg)
Tracks happens‐before: sound & precise 80X slowdown Each analysis step: O(n) time (n = # of threads)
FastTrack [Flanagan & Freund ’09]
Reads & writes (97%): O(1) time Synchronization (3%): O(n) time 8X slowdown
![Page 61: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/61.jpg)
Thread A Thread B
5 2 3 4A B A B
Vector clocks
![Page 62: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/62.jpg)
Thread A Thread B
5 2 3 4A B A B
Vector clocks
Thread A’s logical time Thread B’s logical time
![Page 63: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/63.jpg)
Thread A Thread B
5 2 3 4A B A B
Vector clocks
Last logical time “received” from B
Last logical time “received” from A
![Page 64: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/64.jpg)
5 2 3 4A B A B
Thread A
unlock m
Thread B
lock m6 2Increment
clock
![Page 65: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/65.jpg)
5 2 3 4A B A B
Thread A
unlock m
Thread B
lock m6 2
5 4
5 2
Joinclocks
![Page 66: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/66.jpg)
5 2 3 4A B A B
Thread A
unlock m
Thread B
lock m6 2
5 4 n = # of threads
O(n) time
5 2
![Page 67: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/67.jpg)
Tracks happens‐before: sound & precise 80X slowdown Each analysis step: O(n) time (n = # of threads)
FastTrack [Flanagan & Freund ’09]
Reads & writes (97%): O(1) time Synchronization (3%): O(n) time 8X slowdown
![Page 68: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/68.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
![Page 69: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/69.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5@A
![Page 70: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/70.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5@A
![Page 71: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/71.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
6 2
5@A
5 2
![Page 72: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/72.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
6 2
5@A
5 2
![Page 73: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/73.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
6 2
5 4
5@A
5 2
![Page 74: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/74.jpg)
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5 4
5@A
6 2Happens before?5 2
![Page 75: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/75.jpg)
5@A
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5 4
6 2
4@B
5 2
![Page 76: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/76.jpg)
5@A
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5 4
6 2
Happens before?
4@B
5 2
![Page 77: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/77.jpg)
5@A
Thread A
write x
unlock m
read x
Thread B
lock m
write x
5 2 3 4A B A B
5 4
6 2
Happens before?
4@B
5 2
![Page 78: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/78.jpg)
FastTrack[Flanagan & Freund ’09]
Detection rate occurrence rate
Running time t(c1 + c2n)
No. of threads
![Page 79: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/79.jpg)
FastTrack[Flanagan & Freund ’09]
Detection rate occurrence rate
Running time t(c1 + c2n)
Reads & writes Synchronization
![Page 80: Concurrency - Computer Science and Engineeringweb.cse.ohio-state.edu/~bond.213/6341/Concurrency.pdfThreads Shared, mutable ... Monitor wait & notify Thread start & join 5. Less synchronization](https://reader035.fdocuments.net/reader035/viewer/2022062919/5edf6aa5ad6a402d666ac3b5/html5/thumbnails/80.jpg)
FastTrack[Flanagan & Freund ’09]
Detection rate occurrence rate
Running time t(c1 + c2n)
Reads & writes
Problem today Problem in future
Synchronization