2
2
3
3
For our second project, let’s look at a classic concurrency problem. It’s
4
4
called ‘the dining philosophers’. It was originally conceived by Dijkstra in
5
- 1965, but we’ll use the version from [ this paper] [ paper ] by Tony Hoare in 1985.
5
+ 1965, but we’ll use a lightly adapted version from [ this paper] [ paper ] by Tony
6
+ Hoare in 1985.
6
7
7
8
[ paper ] : http://www.usingcsp.com/cspbook.pdf
8
9
9
10
> In ancient times, a wealthy philanthropist endowed a College to accommodate
10
- > five eminent philosophers. Each philosopher had a room in which she could
11
- > engage in her professional activity of thinking; there was also a common
11
+ > five eminent philosophers. Each philosopher had a room in which they could
12
+ > engage in their professional activity of thinking; there was also a common
12
13
> dining room, furnished with a circular table, surrounded by five chairs, each
13
14
> labelled by the name of the philosopher who was to sit in it. They sat
14
15
> anticlockwise around the table. To the left of each philosopher there was
15
16
> laid a golden fork, and in the centre stood a large bowl of spaghetti, which
16
- > was constantly replenished. A philosopher was expected to spend most of her
17
- > time thinking; but when she felt hungry, she went to the dining room, sat down
18
- > in her own chair, picked up her own fork on her left, and plunged it into the
19
- > spaghetti. But such is the tangled nature of spaghetti that a second fork is
20
- > required to carry it to the mouth. The philosopher therefore had also to pick
21
- > up the fork on her right. When she was finished she would put down both her
22
- > forks, get up from her chair, and continue thinking. Of course, a fork can be
23
- > used by only one philosopher at a time. If the other philosopher wants it, she
24
- > just has to wait until the fork is available again.
17
+ > was constantly replenished. A philosopher was expected to spend most of
18
+ > their time thinking; but when they felt hungry, they went to the dining
19
+ > room, sat down in their own chair, picked up their own fork on their left,
20
+ > and plunged it into the spaghetti. But such is the tangled nature of
21
+ > spaghetti that a second fork is required to carry it to the mouth. The
22
+ > philosopher therefore had also to pick up the fork on their right. When
23
+ > they were finished they would put down both their forks, get up from their
24
+ > chair, and continue thinking. Of course, a fork can be used by only one
25
+ > philosopher at a time. If the other philosopher wants it, they just have
26
+ > to wait until the fork is available again.
25
27
26
28
This classic problem shows off a few different elements of concurrency. The
27
29
reason is that it's actually slightly tricky to implement: a simple
@@ -60,10 +62,10 @@ impl Philosopher {
60
62
}
61
63
62
64
fn main () {
63
- let p1 = Philosopher :: new (" Baruch Spinoza " );
65
+ let p1 = Philosopher :: new (" Judith Butler " );
64
66
let p2 = Philosopher :: new (" Gilles Deleuze" );
65
67
let p3 = Philosopher :: new (" Karl Marx" );
66
- let p4 = Philosopher :: new (" Friedrich Nietzsche " );
68
+ let p4 = Philosopher :: new (" Emma Goldman " );
67
69
let p5 = Philosopher :: new (" Michel Foucault" );
68
70
}
69
71
```
@@ -159,10 +161,10 @@ look at `main()` again:
159
161
# }
160
162
#
161
163
fn main () {
162
- let p1 = Philosopher :: new (" Baruch Spinoza " );
164
+ let p1 = Philosopher :: new (" Judith Butler " );
163
165
let p2 = Philosopher :: new (" Gilles Deleuze" );
164
166
let p3 = Philosopher :: new (" Karl Marx" );
165
- let p4 = Philosopher :: new (" Friedrich Nietzsche " );
167
+ let p4 = Philosopher :: new (" Emma Goldman " );
166
168
let p5 = Philosopher :: new (" Michel Foucault" );
167
169
}
168
170
```
@@ -176,10 +178,10 @@ that `new()` function, it would look like this:
176
178
# name : String ,
177
179
# }
178
180
fn main () {
179
- let p1 = Philosopher { name : " Baruch Spinoza " . to_string () };
181
+ let p1 = Philosopher { name : " Judith Butler " . to_string () };
180
182
let p2 = Philosopher { name : " Gilles Deleuze" . to_string () };
181
183
let p3 = Philosopher { name : " Karl Marx" . to_string () };
182
- let p4 = Philosopher { name : " Friedrich Nietzche " . to_string () };
184
+ let p4 = Philosopher { name : " Emma Goldman " . to_string () };
183
185
let p5 = Philosopher { name : " Michel Foucault" . to_string () };
184
186
}
185
187
```
@@ -211,10 +213,10 @@ impl Philosopher {
211
213
212
214
fn main () {
213
215
let philosophers = vec! [
214
- Philosopher :: new (" Baruch Spinoza " ),
216
+ Philosopher :: new (" Judith Butler " ),
215
217
Philosopher :: new (" Gilles Deleuze" ),
216
218
Philosopher :: new (" Karl Marx" ),
217
- Philosopher :: new (" Friedrich Nietzsche " ),
219
+ Philosopher :: new (" Emma Goldman " ),
218
220
Philosopher :: new (" Michel Foucault" ),
219
221
];
220
222
@@ -247,10 +249,10 @@ mention they’re done eating. Running this program should give you the followin
247
249
output:
248
250
249
251
``` text
250
- Baruch Spinoza is done eating.
252
+ Judith Butler is done eating.
251
253
Gilles Deleuze is done eating.
252
254
Karl Marx is done eating.
253
- Friedrich Nietzsche is done eating.
255
+ Emma Goldman is done eating.
254
256
Michel Foucault is done eating.
255
257
```
256
258
@@ -285,10 +287,10 @@ impl Philosopher {
285
287
286
288
fn main () {
287
289
let philosophers = vec! [
288
- Philosopher :: new (" Baruch Spinoza " ),
290
+ Philosopher :: new (" Judith Butler " ),
289
291
Philosopher :: new (" Gilles Deleuze" ),
290
292
Philosopher :: new (" Karl Marx" ),
291
- Philosopher :: new (" Friedrich Nietzsche " ),
293
+ Philosopher :: new (" Emma Goldman " ),
292
294
Philosopher :: new (" Michel Foucault" ),
293
295
];
294
296
@@ -323,14 +325,14 @@ simulate the time it takes a philosopher to eat.
323
325
If you run this program, you should see each philosopher eat in turn:
324
326
325
327
``` text
326
- Baruch Spinoza is eating.
327
- Baruch Spinoza is done eating.
328
+ Judith Butler is eating.
329
+ Judith Butler is done eating.
328
330
Gilles Deleuze is eating.
329
331
Gilles Deleuze is done eating.
330
332
Karl Marx is eating.
331
333
Karl Marx is done eating.
332
- Friedrich Nietzsche is eating.
333
- Friedrich Nietzsche is done eating.
334
+ Emma Goldman is eating.
335
+ Emma Goldman is done eating.
334
336
Michel Foucault is eating.
335
337
Michel Foucault is done eating.
336
338
```
@@ -366,10 +368,10 @@ impl Philosopher {
366
368
367
369
fn main () {
368
370
let philosophers = vec! [
369
- Philosopher :: new (" Baruch Spinoza " ),
371
+ Philosopher :: new (" Judith Butler " ),
370
372
Philosopher :: new (" Gilles Deleuze" ),
371
373
Philosopher :: new (" Karl Marx" ),
372
- Philosopher :: new (" Friedrich Nietzsche " ),
374
+ Philosopher :: new (" Emma Goldman " ),
373
375
Philosopher :: new (" Michel Foucault" ),
374
376
];
375
377
@@ -458,11 +460,11 @@ We have multi-threading!
458
460
``` text
459
461
Gilles Deleuze is eating.
460
462
Gilles Deleuze is done eating.
461
- Friedrich Nietzsche is eating.
462
- Friedrich Nietzsche is done eating.
463
+ Emma Goldman is eating.
464
+ Emma Goldman is done eating.
463
465
Michel Foucault is eating.
464
- Baruch Spinoza is eating.
465
- Baruch Spinoza is done eating.
466
+ Judith Butler is eating.
467
+ Judith Butler is done eating.
466
468
Karl Marx is eating.
467
469
Karl Marx is done eating.
468
470
Michel Foucault is done eating.
@@ -532,10 +534,10 @@ fn main() {
532
534
]});
533
535
534
536
let philosophers = vec! [
535
- Philosopher :: new (" Baruch Spinoza " , 0 , 1 ),
537
+ Philosopher :: new (" Judith Butler " , 0 , 1 ),
536
538
Philosopher :: new (" Gilles Deleuze" , 1 , 2 ),
537
539
Philosopher :: new (" Karl Marx" , 2 , 3 ),
538
- Philosopher :: new (" Friedrich Nietzsche " , 3 , 4 ),
540
+ Philosopher :: new (" Emma Goldman " , 3 , 4 ),
539
541
Philosopher :: new (" Michel Foucault" , 0 , 4 ),
540
542
];
541
543
@@ -643,10 +645,10 @@ count will go up, and when each thread ends, it will go back down.
643
645
644
646
``` rust,ignore
645
647
let philosophers = vec![
646
- Philosopher::new("Baruch Spinoza ", 0, 1),
648
+ Philosopher::new("Judith Butler ", 0, 1),
647
649
Philosopher::new("Gilles Deleuze", 1, 2),
648
650
Philosopher::new("Karl Marx", 2, 3),
649
- Philosopher::new("Friedrich Nietzsche ", 3, 4),
651
+ Philosopher::new("Emma Goldman ", 3, 4),
650
652
Philosopher::new("Michel Foucault", 0, 4),
651
653
];
652
654
```
@@ -679,12 +681,12 @@ and so you’ll get some output like this:
679
681
680
682
``` text
681
683
Gilles Deleuze is eating.
682
- Friedrich Nietzsche is eating.
683
- Friedrich Nietzsche is done eating.
684
+ Emma Goldman is eating.
685
+ Emma Goldman is done eating.
684
686
Gilles Deleuze is done eating.
685
- Baruch Spinoza is eating.
687
+ Judith Butler is eating.
686
688
Karl Marx is eating.
687
- Baruch Spinoza is done eating.
689
+ Judith Butler is done eating.
688
690
Michel Foucault is eating.
689
691
Karl Marx is done eating.
690
692
Michel Foucault is done eating.
0 commit comments