synchronization
DESCRIPTION
Synchronization. Motivating Example: Too Much Milk. Two robots are programmed to maintain the milk inventory at a store… They are not aware of each other’s presence…. Robot: Dumber. Robot: Dumb. Dumb 4:00Look into fridge: Out of milk. Dumber. Motivating Example: Too Much Milk. - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/2.jpg)
Motivating Example: Too Much Milk
Two robots are programmed to maintain the milk inventory at a store…
They are not aware of each other’s presence…
Robot: Dumb Robot: Dumber
![Page 3: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/3.jpg)
Motivating Example: Too Much Milk
Dumb4:00 Look into fridge:
Out of milk
Dumber
![Page 4: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/4.jpg)
Motivating Example: Too Much Milk
Dumb4:00 Look into fridge:
Out of milk4:05 Head for the
warehouse
Dumber
![Page 5: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/5.jpg)
Motivating Example: Too Much Milk
Dumb4:05 Head for the
warehouse
Dumber
4:10 Look into fridge: Out of milk
![Page 6: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/6.jpg)
Motivating Example: Too Much Milk
Dumb Dumber4:10 Look into fridge:
Out of milk4:15 Head for the
warehouse
![Page 7: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/7.jpg)
Motivating Example: Too Much Milk
Dumb
4:20 Arrive with milk
Dumber4:15 Head for the
warehouse
![Page 8: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/8.jpg)
Motivating Example: Too Much Milk
Dumb
4:20 Arrive with milk
Dumber4:15 Head for the
warehouse
![Page 9: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/9.jpg)
Motivating Example: Too Much Milk
Dumb4:20 Arrive with milk4:25 Go party
Dumber
![Page 10: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/10.jpg)
Motivating Example: Too Much Milk
Dumb4:20 Arrive with milk4:25 Go party
Dumber
4:30 Arrive with milk: “Uh oh…”
![Page 11: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/11.jpg)
Definitions
Synchronization: use atomic operations to ensure cooperation among threads
Mutual exclusion: ensure one thread can do something without the interference of other threads
Critical section: a piece of code that only one thread can execute at a time
![Page 12: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/12.jpg)
More on Critical Section
A lock prevents a thread from doing something A thread should lock before entering a critical
section A thread should unlock when leaving the
critical section A thread should wait if the critical section is
locked Synchronization often involves waiting
![Page 13: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/13.jpg)
Too Much Milk: Solution 1
Two properties: Someone should go get the milk if needed Only one robot will go get milk
Basic idea of solution 1 Leave a note (kind of like a lock) Remove the note (kind of like a unlock) Don’t go get milk if the note is around (wait)
![Page 14: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/14.jpg)
Too Much Milk: Solution 1
if (no milk) {if (no note) {
// leave a note;// go get milk;// remove the note;
}}
![Page 15: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/15.jpg)
Too Much Milk: Solution 1
Dumb4:00 if (no milk) {
Dumber
![Page 16: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/16.jpg)
Too Much Milk: Solution 1
Dumb4:00 if (no milk) {
Dumber
4:01 if (no milk) {
![Page 17: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/17.jpg)
Too Much Milk: Solution 1
Dumb4:00 if (no milk) {
Dumber
4:01 if (no milk) {4:02 if (no note) {
![Page 18: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/18.jpg)
Too Much Milk: Solution 1
Dumb4:00 if (no milk) {
4:03 if (no note) {
Dumber
4:01 if (no milk) {4:02 if (no note) {
![Page 19: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/19.jpg)
Too Much Milk: Solution 1
Dumb4:00 if (no milk) {
4:03 if (no note) {4:04 // leave a note
Dumber
4:01 if (no milk) {4:02 if (no note) {
![Page 20: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/20.jpg)
Too Much Milk: Solution 1
Dumb
4:03 if (no note) {4:04 // leave a note
Dumber4:01 if (no milk) {4:02 if (no note) {
4:05 // leave a note
![Page 21: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/21.jpg)
Too Much Milk: Solution 1
Dumb
4:03 if (no note) {4:04 // leave a note
4:06 // go get milk
Dumber4:02 if (no note) {
4:05 // leave a note
![Page 22: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/22.jpg)
Too Much Milk: Solution 1
Dumb4:03 if (no note) {4:04 // leave a note
4:06 // go get milk
Dumber
4:05 // leave a note
4:07 // go get milk
![Page 23: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/23.jpg)
Too Much Milk: Solution 2
Okay…solution 1 does not work The notes are posted too late… What if both robots begin by leaving their own
notes?
![Page 24: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/24.jpg)
Too Much Milk: Solution 2
// leave a note; if (no note from the other) {if (no milk) {
// go get milk;}
} // remove the note;
![Page 25: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/25.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
Dumber
![Page 26: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/26.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
Dumber
4:01 // leave a note
![Page 27: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/27.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
4:02 if (no note from Dumber) {…}
Dumber
4:01 // leave a note
![Page 28: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/28.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
4:02 if (no note from Dumber) {…}
Dumber
4:01 // leave a note
4:03 if (no note from Dumb) {…}
![Page 29: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/29.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
4:02 if (no note from Dumber) {…}
4:04 // remove the note
Dumber
4:01 // leave a note
4:03 if (no note from Dumb) {…}
![Page 30: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/30.jpg)
Too Much Milk: Solution 2
Dumb4:00 // leave a note
4:02 if (no note from Dumber) {…}
4:04 // remove the note
Dumber
4:01 // leave a note
4:03 if (no note from Dumb) {…}
![Page 31: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/31.jpg)
Too Much Milk: Solution 2
Dumb
4:02 if (no note from Dumber) {…}
4:04 // remove the note
Dumber4:01 // leave a note
4:03 if (no note from Dumb) {…}
4:05 // remove the note
![Page 32: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/32.jpg)
Too Much Milk: Solution 2
Dumb
4:02 if (no note from Dumber) {…}
4:04 // remove the note
Dumber4:01 // leave a note
4:03 if (no note from Dumb) {…}
4:05 // remove the note
![Page 33: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/33.jpg)
Too Much Milk: Solution 3
Dumb// leave Dumb’s notewhile (Dumber’s note) { };if (no milk) {
// go get milk}// remove Dumb’s note
Dumber// leave Dumber’s noteif (no Dumb’s note) { if (no milk) { // go get milk }}// remove Dumber’s note
![Page 34: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/34.jpg)
Too Much Milk Solution 3
How do we verify the correctness of a solution?
Test arbitrary interleaving of locking and checking locks In this case, leaving notes and checking notes
![Page 35: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/35.jpg)
Dumber Challenges Dumb: Case 1
Dumb// leave Dumb’s notewhile (Dumber’s note) { };
if (no milk) {// go get milk
}
// remove Dumb’s note
Dumber
// leave Dumber’s note
if (no Dumb’s note) {}// remove Dumber’s note
Tim
e
![Page 36: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/36.jpg)
Dumber Challenges Dumb: Case 2
Dumb// leave Dumb’s note
while (Dumber’s note) { };
if (no milk) {// go get milk
}// remove Dumb’s note
Dumber
// leave Dumber’s note
if (no Dumb’s note) {}// remove Dumber’s note
Tim
e
![Page 37: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/37.jpg)
Dumber Challenges Dumb: Case 3
Dumb// leave Dumb’s note
while (Dumber’s note) { };
if (no milk) {}// remove Dumb’s note
Dumber
// leave Dumber’s noteif (no Dumb’s note) {}// remove Dumber’s note
Tim
e
![Page 38: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/38.jpg)
Dumb Challenges Dumber: Case 1
Dumb
// leave Dumb’s notewhile (Dumber’s note) { };
if (no milk) {}// remove Dumb’s note
Dumber// leave Dumber’s noteif (no Dumb’s note) {
if (no milk) { // go get milk }}// remove Dumber’s note
Tim
e
![Page 39: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/39.jpg)
Dumb Challenges Dumber: Case 2
Dumb
// leave Dumb’s note
while (Dumber’s note) { };
if (no milk) { // go get milk}// remove Dumb’s note
Dumber// leave Dumber’s note
if (no Dumb’s note) {}// remove Dumber’s note
Tim
e
![Page 40: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/40.jpg)
Dumb Challenges Dumber: Case 3
Dumb
// leave Dumb’s notewhile (Dumber’s note) { };
if (no milk) { // go get milk}// remove Dumb’s note
Dumber// leave Dumber’s note
if (no Dumb’s note) {}// remove Dumber’s note
Tim
e
![Page 41: Synchronization](https://reader034.vdocuments.us/reader034/viewer/2022051700/56816861550346895ddeb0df/html5/thumbnails/41.jpg)
Lessons Learned Although it works, Solution 3 is ugly
Difficult to verify correctness Two threads have different code
Difficult to generalize to N threads While Dumb is waiting, it consumes CPU time
(busy waiting) More elegant with higher-level primitives
lockacquire();if (no milk) { // go get milk }lockrelease();