Managing Milk Mishaps: A Concurrency Lesson

undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 1
 
Too Much Milk
 
R
o
o
m
m
a
t
e
 
A
3
:
0
0
A
r
r
i
v
e
 
h
o
m
e
:
 
n
o
 
m
i
l
k
3
:
0
5
L
e
a
v
e
 
f
o
r
 
s
t
o
r
e
3
:
1
0
A
r
r
i
v
e
 
a
t
 
s
t
o
r
e
3
:
1
5
L
e
a
v
e
 
s
t
o
r
e
3
:
2
0
A
r
r
i
v
e
 
h
o
m
e
,
 
p
u
t
 
m
i
l
k
 
a
w
a
y
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 2
 
Too Much Milk
 
R
o
o
m
m
a
t
e
 
A
R
o
o
m
m
a
t
e
 
B
3
:
0
0
A
r
r
i
v
e
 
h
o
m
e
:
 
n
o
 
m
i
l
k
3
:
0
5
L
e
a
v
e
 
f
o
r
 
s
t
o
r
e
3
:
1
0
A
r
r
i
v
e
 
a
t
 
s
t
o
r
e
A
r
r
i
v
e
 
h
o
m
e
:
 
n
o
 
m
i
l
k
3
:
1
5
L
e
a
v
e
 
s
t
o
r
e
L
e
a
v
e
 
f
o
r
 
s
t
o
r
e
3
:
2
0
A
r
r
i
v
e
 
h
o
m
e
,
 
p
u
t
 
m
i
l
k
 
a
w
a
y
A
r
r
i
v
e
 
a
t
 
s
t
o
r
e
3
:
2
5
L
e
a
v
e
 
s
t
o
r
e
3
:
3
0
A
r
r
i
v
e
 
h
o
m
e
:
 
t
o
o
 
m
u
c
h
 
m
i
l
k
!
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 3
 
Computerized Milk Purchase
 
1
 if (milk == 0) {
2
   if (note == 0) {
3
     note = 1;
4
     buy_milk();
5
     note = 0;
6
   }
7
 }
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 4
 
Still Too Much Milk
 
 
 
 
 
 
 
T
h
r
e
a
d
 
A
:
T
h
r
e
a
d
 
B
:
1
  if (milk == 0) {
2
    if (note == 0) {
3
   
if (milk == 0) {
4
  
  if (note == 0) {
5
  
    note = 1;
6
   
    buy_milk();
7
   
    note = 0;
8
   
  }
9
   
}
10
     note = 1;
11
     buy_milk();
12
     note = 0;
13
   }
14
 }
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 5
 
Second Attempt
 
T
h
r
e
a
d
 
A
:
 
1
 if (note == 0) {
2
   if (milk == 0) {
3
     buy_milk();
4
   }
5
   note = 1;
6
 }
 
T
h
r
e
a
d
 
B
:
 
1
 if (note == 1) {
2
   if (milk == 0) {
3
     buy_milk();
4
   }
5
   note = 0;
6
 }
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 6
 
Third Attempt
 
T
h
r
e
a
d
 
A
:
 
1
 noteA = 1;
2
 if (noteB == 0) {
3
   if (milk == 0) {
4
     buy_milk();
5
   }
6
 }
7
 
noteA = 0;
 
T
h
r
e
a
d
 
B
:
 
1
 noteB = 1;
2
 if (noteA == 0) {
3
   if (milk == 0) {
4
     buy_milk();
5
   }
6
 }
7
 
noteB = 0;
undefined
 
CS 140 Lecture Notes: Concurrency
 
Slide 7
 
Fourth Attempt
 
T
h
r
e
a
d
 
A
:
 
1
 noteA = 1;
2
 if (noteB == 0) {
3
   if (milk == 0) {
4
     buy_milk();
5
   }
6
 }
7
 
noteA = 0;
 
T
h
r
e
a
d
 
B
:
 
1
 noteB = 1;
2
 while (noteA == 1) {
3
   // do nothing
4
 }
5
 if (milk == 0) {
6
   buy_milk();
7
 }
8
 
noteB = 0;
Slide Note
Embed
Share

Learn about the comical scenario of roommates forgetting to buy milk, and delve into concurrency concepts through humorous anecdotes and code snippets. The lecture slides depict the chaotic yet educational journey of handling too much milk through synchronized threads and computerized milk purchases.

  • Concurrency
  • Milk Mishaps
  • Roommates
  • Lecture Notes
  • Programming

Uploaded on Aug 24, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


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

  2. Too Much Milk Roommate A Roommate 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 Slide 2

  3. Computerized 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

  4. Still Too Much Milk Thread A: 1 if (milk == 0) { 2 if (note == 0) { 3 4 5 6 7 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

  5. Second Attempt Thread A: Thread B: 1 if (note == 0) { 2 if (milk == 0) { 3 buy_milk(); 4 } 5 note = 1; 6 } 1 if (note == 1) { 2 if (milk == 0) { 3 buy_milk(); 4 } 5 note = 0; 6 } CS 140 Lecture Notes: Concurrency Slide 5

  6. Third Attempt Thread A: Thread B: 1 noteA = 1; 2 if (noteB == 0) { 3 if (milk == 0) { 4 buy_milk(); 5 } 6 } 7 noteA = 0; 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

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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#