Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive.

Similar presentations


Presentation on theme: "CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive."— Presentation transcript:

1 CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive home, put milk away

2 CS 140 Lecture Notes: ConcurrencySlide 2 Too Much Milk Roomate ARoomate B 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at storeArrive home: no milk 3:15Leave storeLeave for store 3:20Arrive home, put milk awayArrive at store 3:25Leave store 3:30Arrive home: too much milk!

3 CS 140 Lecture Notes: ConcurrencySlide 3 Computerized Milk Purchase if (milk == 0) { if (note == 0) { note = 1; buyMilk(); note = 0; }

4 CS 140 Lecture Notes: ConcurrencySlide 4 Still Too Much Milk Thread A:Thread B: if (milk == 0) { if (note == 0) { if (milk == 0) { if (note == 0) { note = 1; buyMilk(); note = 0; } note = 1; buyMilk(); note = 0; }

5 CS 140 Lecture Notes: ConcurrencySlide 5 Second Attempt Thread A: if (note == 0) { if (milk == 0) { buyMilk(); } note = 1; } Thread B: if (note == 1) { if (milk == 0) { buyMilk(); } note = 0; }

6 CS 140 Lecture Notes: ConcurrencySlide 6 Third Attempt Thread A: 1 noteA = 1; 2 if (noteB == 0) { 3 if (milk == 0) { 4 buyMilk(); 5 } 6 } 7noteA = 0; Thread B: 1 noteB = 1; 2 if (noteA == 0) { 3 if (milk == 0) { 4 buyMilk(); 5 } 6 } 7noteB = 0;

7 CS 140 Lecture Notes: ConcurrencySlide 7 Fourth Attempt Thread A: 1 noteA = 1; 2 if (noteB == 0) { 3 if (milk == 0) { 4 buyMilk(); 5 } 6 } 7noteA = 0; Thread B: 1 noteB = 1; 2 while (noteA == 1) { 3 // do nothing 4 } 5 if (milk == 0) { 6 buyMilk(); 7 } 8noteB = 0;

8 CS 140 Lecture Notes: ConcurrencySlide 8


Download ppt "CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive."

Similar presentations


Ads by Google