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