-
Notifications
You must be signed in to change notification settings - Fork 0
/
Livelock.cs
94 lines (81 loc) · 2.57 KB
/
Livelock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ThreadsAndProblems
{
class Livelock
{
public static void Main(string[] args)
{
var husband = new Diner("Bob");
var wife = new Diner("Alice");
var spool = new Spoon(husband);
Task.WaitAll(
Task.Run(() => husband.EatWith(spool, wife)),
Task.Run(() => wife.EatWith(spool, husband))
);
}
public class Spoon
{
public Diner Owner { get; private set; }
public Spoon(Diner diner)
{
Owner = diner;
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void SetOwner(Diner d)
{
Owner = d;
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Use()
{
Console.WriteLine("{0} has eaten!", Owner.Name);
}
}
public class Diner
{
public string Name { get; private set; }
private bool IsHungry { get; set; }
public Diner(string n)
{
Name = n;
IsHungry = true;
}
public void EatWith(Spoon spoon, Diner spouse)
{
while (IsHungry)
{
// Don't have the spoon, so wait patiently for spouse.
if (spoon.Owner != this)
{
try
{
Thread.Sleep(1);
}
catch (ThreadInterruptedException e)
{
}
continue;
}
// If spouse is hungry, insist upon passing the spoon.
if (spouse.IsHungry)
{
Console.WriteLine("{0}: You eat first my darling {1}!", Name, spouse.Name);
spoon.SetOwner(spouse);
continue;
}
// Spouse wasn't hungry, so finally eat
spoon.Use();
IsHungry = false;
Console.WriteLine("{0}: I am stuffed, my darling {1}!", Name, spouse.Name);
spoon.SetOwner(spouse);
}
}
}
}
}