1 / 8

Managing Milk CONCURRENCY in Roommates: A CS Theory Exploration

This document explores a concurrency problem faced by two roommates dealing with milk purchases, specifically analyzing scenarios where insufficient milk triggers a race condition between concurrent threads. It outlines the sequence of events where both roommates independently check for milk and execute a purchase function while managing shared state through notes. Various attempts at implementing thread safety in their milk-buying logic demonstrate the challenges and solutions in ensuring that only one roommate buys milk at a time, preventing oversupply.

heath
Télécharger la présentation

Managing Milk CONCURRENCY in Roommates: A CS Theory Exploration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Too Much Milk Person 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

  2. 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

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

  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; } } CS 140 Lecture Notes: Concurrency

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

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

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

  8. CS 140 Lecture Notes: Concurrency

More Related