80 likes | 162 Views
Follow the chaotic journey of two roommates as they navigate the challenges of running out of milk and implementing concurrency in solving the milk crisis. Learn about their comical attempts, mistakes, and eventual success in procuring the much-needed milk.
E N D
Too Much Milk Roomate A 3:00 Arrive home: no milk 3:05 Leave for store 3:10 Arrive at store 3:15 Leave store 3:20 Arrive home, put milk away CS 140 Lecture Notes: Concurrency
Too Much Milk Roomate ARoomate B 3:00 Arrive home: no milk 3:05 Leave for store 3:10 Arrive at store Arrive home: no milk 3:15 Leave store Leave for store 3:20 Arrive home, put milk away Arrive at store 3:25 Leave store 3:30 Arrive home: too much milk! CS 140 Lecture Notes: Concurrency
Computerized Milk Purchase 1 if (milk == 0) { 2if (note == 0) { 3note = 1; 4buy_milk(); 5note = 0; 6} 7 } CS 140 Lecture Notes: Concurrency
Still Too Much Milk Thread A:Thread B: if (milk == 0) { if (note == 0) { if (milk == 0) { if (note == 0) { note = 1; buy_milk(); note = 0; } } note = 1; buy_milk(); note = 0; } } CS 140 Lecture Notes: Concurrency
Thread A: if (note == 0) { if (milk == 0) { buy_milk(); } note = 1; } Thread B: if (note == 1) { if (milk == 0) { buy_milk(); } note = 0; } Second Attempt CS 140 Lecture Notes: Concurrency
Thread A: 1noteA = 1; 2 if (noteB == 0) { 3 if (milk == 0) { 4buy_milk(); 5 } 6 } 7noteA = 0; Thread B: 1noteB = 1; 2 if (noteA == 0) { 3 if (milk == 0) { 4buy_milk(); 5 } 6 } 7noteB = 0; Third Attempt CS 140 Lecture Notes: Concurrency
Thread A: 1noteA = 1; 2 if (noteB == 0) { 3 if (milk == 0) { 4buy_milk(); 5 } 6 } 7noteA = 0; Thread B: 1noteB = 1; 2 while (noteA == 1) { 3// do nothing 4 } 5 if (milk == 0) { 6buy_milk(); 7 } 8noteB = 0; Fourth Attempt CS 140 Lecture Notes: Concurrency