22
33For our second project, let’s look at a classic concurrency problem. It’s
44called ‘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.
67
78[ paper ] : http://www.usingcsp.com/cspbook.pdf
89
910> 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
1213> dining room, furnished with a circular table, surrounded by five chairs, each
1314> labelled by the name of the philosopher who was to sit in it. They sat
1415> anticlockwise around the table. To the left of each philosopher there was
1516> 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 was 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.
2527
2628This classic problem shows off a few different elements of concurrency. The
2729reason is that it's actually slightly tricky to implement: a simple
@@ -60,10 +62,10 @@ impl Philosopher {
6062}
6163
6264fn main () {
63- let p1 = Philosopher :: new (" Baruch Spinoza " );
65+ let p1 = Philosopher :: new (" Judith Butler " );
6466 let p2 = Philosopher :: new (" Gilles Deleuze" );
6567 let p3 = Philosopher :: new (" Karl Marx" );
66- let p4 = Philosopher :: new (" Friedrich Nietzsche " );
68+ let p4 = Philosopher :: new (" Emma Goldman " );
6769 let p5 = Philosopher :: new (" Michel Foucault" );
6870}
6971```
@@ -159,10 +161,10 @@ look at `main()` again:
159161# }
160162#
161163fn main () {
162- let p1 = Philosopher :: new (" Baruch Spinoza " );
164+ let p1 = Philosopher :: new (" Judith Butler " );
163165 let p2 = Philosopher :: new (" Gilles Deleuze" );
164166 let p3 = Philosopher :: new (" Karl Marx" );
165- let p4 = Philosopher :: new (" Friedrich Nietzsche " );
167+ let p4 = Philosopher :: new (" Emma Goldman " );
166168 let p5 = Philosopher :: new (" Michel Foucault" );
167169}
168170```
@@ -176,10 +178,10 @@ that `new()` function, it would look like this:
176178# name : String ,
177179# }
178180fn main () {
179- let p1 = Philosopher { name : " Baruch Spinoza " . to_string () };
181+ let p1 = Philosopher { name : " Judith Butler " . to_string () };
180182 let p2 = Philosopher { name : " Gilles Deleuze" . to_string () };
181183 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 () };
183185 let p5 = Philosopher { name : " Michel Foucault" . to_string () };
184186}
185187```
@@ -211,10 +213,10 @@ impl Philosopher {
211213
212214fn main () {
213215 let philosophers = vec! [
214- Philosopher :: new (" Baruch Spinoza " ),
216+ Philosopher :: new (" Judith Butler " ),
215217 Philosopher :: new (" Gilles Deleuze" ),
216218 Philosopher :: new (" Karl Marx" ),
217- Philosopher :: new (" Friedrich Nietzsche " ),
219+ Philosopher :: new (" Emma Goldman " ),
218220 Philosopher :: new (" Michel Foucault" ),
219221 ];
220222
@@ -247,10 +249,10 @@ mention they’re done eating. Running this program should give you the followin
247249output:
248250
249251``` text
250- Baruch Spinoza is done eating.
252+ Judith Butler is done eating.
251253Gilles Deleuze is done eating.
252254Karl Marx is done eating.
253- Friedrich Nietzsche is done eating.
255+ Emma Goldman is done eating.
254256Michel Foucault is done eating.
255257```
256258
@@ -285,10 +287,10 @@ impl Philosopher {
285287
286288fn main () {
287289 let philosophers = vec! [
288- Philosopher :: new (" Baruch Spinoza " ),
290+ Philosopher :: new (" Judith Butler " ),
289291 Philosopher :: new (" Gilles Deleuze" ),
290292 Philosopher :: new (" Karl Marx" ),
291- Philosopher :: new (" Friedrich Nietzsche " ),
293+ Philosopher :: new (" Emma Goldman " ),
292294 Philosopher :: new (" Michel Foucault" ),
293295 ];
294296
@@ -323,14 +325,14 @@ simulate the time it takes a philosopher to eat.
323325If you run this program, you should see each philosopher eat in turn:
324326
325327``` text
326- Baruch Spinoza is eating.
327- Baruch Spinoza is done eating.
328+ Judith Butler is eating.
329+ Judith Butler is done eating.
328330Gilles Deleuze is eating.
329331Gilles Deleuze is done eating.
330332Karl Marx is eating.
331333Karl 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.
334336Michel Foucault is eating.
335337Michel Foucault is done eating.
336338```
@@ -366,10 +368,10 @@ impl Philosopher {
366368
367369fn main () {
368370 let philosophers = vec! [
369- Philosopher :: new (" Baruch Spinoza " ),
371+ Philosopher :: new (" Judith Butler " ),
370372 Philosopher :: new (" Gilles Deleuze" ),
371373 Philosopher :: new (" Karl Marx" ),
372- Philosopher :: new (" Friedrich Nietzsche " ),
374+ Philosopher :: new (" Emma Goldman " ),
373375 Philosopher :: new (" Michel Foucault" ),
374376 ];
375377
@@ -458,11 +460,11 @@ We have multi-threading!
458460``` text
459461Gilles Deleuze is eating.
460462Gilles 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.
463465Michel 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.
466468Karl Marx is eating.
467469Karl Marx is done eating.
468470Michel Foucault is done eating.
@@ -532,10 +534,10 @@ fn main() {
532534 ]});
533535
534536 let philosophers = vec! [
535- Philosopher :: new (" Baruch Spinoza " , 0 , 1 ),
537+ Philosopher :: new (" Judith Butler " , 0 , 1 ),
536538 Philosopher :: new (" Gilles Deleuze" , 1 , 2 ),
537539 Philosopher :: new (" Karl Marx" , 2 , 3 ),
538- Philosopher :: new (" Friedrich Nietzsche " , 3 , 4 ),
540+ Philosopher :: new (" Emma Goldman " , 3 , 4 ),
539541 Philosopher :: new (" Michel Foucault" , 0 , 4 ),
540542 ];
541543
@@ -643,10 +645,10 @@ count will go up, and when each thread ends, it will go back down.
643645
644646``` rust,ignore
645647let philosophers = vec![
646- Philosopher::new("Baruch Spinoza ", 0, 1),
648+ Philosopher::new("Judith Butler ", 0, 1),
647649 Philosopher::new("Gilles Deleuze", 1, 2),
648650 Philosopher::new("Karl Marx", 2, 3),
649- Philosopher::new("Friedrich Nietzsche ", 3, 4),
651+ Philosopher::new("Emma Goldman ", 3, 4),
650652 Philosopher::new("Michel Foucault", 0, 4),
651653];
652654```
@@ -679,12 +681,12 @@ and so you’ll get some output like this:
679681
680682``` text
681683Gilles 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.
684686Gilles Deleuze is done eating.
685- Baruch Spinoza is eating.
687+ Judith Butler is eating.
686688Karl Marx is eating.
687- Baruch Spinoza is done eating.
689+ Judith Butler is done eating.
688690Michel Foucault is eating.
689691Karl Marx is done eating.
690692Michel Foucault is done eating.
0 commit comments