Slide 1 Too Much Milk Person A 300 Arrive home no milk 305 Leave for store 310 Arrive at store 315 Leave store 320 Arrive home put milk away CS 140 Lecture Notes Concurrency ID: 166990
Download Presentation The PPT/PDF document "CS 140 Lecture Notes: Concurrency" 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
CS 140 Lecture Notes: Concurrency
Slide 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 awaySlide2
CS 140 Lecture Notes: Concurrency
Slide 2
Too Much Milk
Roomate
A
Roomate
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!Slide3
CS 140 Lecture Notes: Concurrency
Slide 3
Computerized Milk Purchase
if
(milk == 0)
{
if
(note == 0)
{
note = 1;
buyMilk
();
note = 0;
}
} Slide4
CS 140 Lecture Notes: Concurrency
Slide 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;
}
} Slide5
CS 140 Lecture Notes: Concurrency
Slide 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;
}Slide6
CS 140 Lecture Notes: Concurrency
Slide 6
Third Attempt
Thread A:
1
noteA
= 1;
2
if
(
noteB
== 0)
{
3
if
(milk == 0)
{
4
buyMilk
();
5
}
6 }7 noteA = 0;
Thread B:
1
noteB
= 1;
2
if
(
noteA
== 0)
{
3
if
(milk == 0)
{
4
buyMilk
();
5
}
6
}
7
noteB
= 0;Slide7
CS 140 Lecture Notes: Concurrency
Slide 7
Fourth Attempt
Thread A:
1
noteA
= 1;
2
if
(
noteB
== 0)
{
3
if
(milk == 0)
{
4
buyMilk
();
5
}
6 }7 noteA = 0;
Thread B:
1
noteB
= 1;
2
while
(
noteA
== 1)
{
3
// do
nothing
4
}
5
if
(milk == 0)
{
6
buyMilk
();
7
}
8
noteB
= 0;Slide8
CS 140 Lecture Notes: Concurrency
Slide 8