300 Arrive home no milk 305 Leave for store 310 Arrive at store 315 Leave store 320 Arrive home put milk away 325 330 CS 140 Lecture Notes Concurrency Slide 1 Too Much Milk ID: 795343
Download The PPT/PDF document "Too Much Milk Roommate A" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Too Much Milk
Roommate A3:00 Arrive home: no milk3:05 Leave for store3:10 Arrive at store3:15 Leave store3:20 Arrive home, put milk away3:253:30
CS 140 Lecture Notes: Concurrency
Slide 1
Slide2Too Much Milk
Roommate A3:00 Arrive home: no milk3:05 Leave for store3:10 Arrive at store3:15 Leave store3:20 Arrive home, put milk away3:253:30
Roommate B
Arrive home: no milkLeave for storeArrive at store
Leave store
Arrive home:
too much milk!
CS 140 Lecture Notes: Concurrency
Slide
2
Slide3Computerized Milk Purchase
1 if (milk == 0) {2 if (note == 0) {3
note = 1;4
buy_milk();
5
note = 0;
6
}
7
}
CS 140 Lecture Notes: Concurrency
Slide
3
Slide4Still Too Much Milk
Thread A:1 if (milk == 0) {2 if (note == 0) {3
45
67
8
9
10
note = 1;
11
buy_milk
();
12
note = 0;
13 }
14
}
Thread B:if (milk == 0) { if (note == 0) { note = 1; buy_milk(); note = 0; }}
CS 140 Lecture Notes: Concurrency
Slide
4
Slide5Second Attempt
Thread A:1 if (note == 0) {2 if (milk == 0) {3 buy_milk
();4
}5 note = 1;
6
}
Thread B:
1
if (note == 1) {
2
if (milk == 0) {
3
buy_milk
();
4
}
5
note = 0;6 }CS 140 Lecture Notes: ConcurrencySlide 5
Slide6Third Attempt
Thread A:1 noteA = 1;2 if (noteB == 0) {
3 if (milk == 0) {
4
buy_milk
();
5
}
6
}
7
noteA = 0;
Thread B:
1
noteB
= 1;
2 if (noteA == 0) {3 if (milk == 0) {4 buy_milk();5 }6 }7 noteB = 0;
CS 140 Lecture Notes: Concurrency
Slide
6
Slide7Fourth Attempt
Thread A:1 noteA = 1;2 if (noteB == 0) {
3 if (milk == 0) {
4
buy_milk
();
5
}
6
}
7
noteA = 0;
Thread B:
1
noteB
= 1;
2 while (noteA == 1) {3 // do nothing4 }5 if (milk == 0) {6 buy_milk();7 }8
noteB = 0;
CS 140 Lecture Notes: Concurrency
Slide
7
Slide8CS 140 Lecture Notes: Concurrency
Slide 8