Skip to content

Illustrate deadlock in dining-philosophers.md #26769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/doc/trpl/dining-philosophers.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ Michel Foucault is done eating.
Easy enough, they’re all done! We haven’t actually implemented the real problem
yet, though, so we’re not done yet!

Next, we want to make our philosophers not just finish eating, but actually
eat. Here’s the next version:
For our philosophers to finish eating they first must have started eating. Here’s
the next version:

```rust
use std::thread;
Expand All @@ -277,7 +277,7 @@ impl Philosopher {
}

fn eat(&self) {
println!("{} is eating.", self.name);
println!("{} is about to eat.", self.name);

thread::sleep_ms(1000);

Expand Down Expand Up @@ -311,7 +311,7 @@ from the standard library, and so we need to `use` it.

```rust,ignore
fn eat(&self) {
println!("{} is eating.", self.name);
println!("{} is about to eat.", self.name);

thread::sleep_ms(1000);

Expand All @@ -325,15 +325,15 @@ simulate the time it takes a philosopher to eat.
If you run this program, you should see each philosopher eat in turn:

```text
Judith Butler is eating.
Judith Butler is about to eat.
Judith Butler is done eating.
Gilles Deleuze is eating.
Gilles Deleuze is about to eat.
Gilles Deleuze is done eating.
Karl Marx is eating.
Karl Marx is about to eat.
Karl Marx is done eating.
Emma Goldman is eating.
Emma Goldman is about to eat.
Emma Goldman is done eating.
Michel Foucault is eating.
Michel Foucault is about to eat.
Michel Foucault is done eating.
```

Expand All @@ -358,7 +358,7 @@ impl Philosopher {
}

fn eat(&self) {
println!("{} is eating.", self.name);
println!("{} is about to eat.", self.name);

thread::sleep_ms(1000);

Expand Down Expand Up @@ -460,14 +460,14 @@ If you run this program, you’ll see that the philosophers eat out of order!
We have multi-threading!

```text
Gilles Deleuze is eating.
Gilles Deleuze is about to eat.
Gilles Deleuze is done eating.
Emma Goldman is eating.
Emma Goldman is about to eat.
Emma Goldman is done eating.
Michel Foucault is eating.
Judith Butler is eating.
Michel Foucault is about to eat.
Judith Butler is about to eat.
Judith Butler is done eating.
Karl Marx is eating.
Karl Marx is about to eat.
Karl Marx is done eating.
Michel Foucault is done eating.
```
Expand Down Expand Up @@ -511,13 +511,12 @@ impl Philosopher {
}

fn eat(&self, table: &Table) {
println!("{} is about to eat.", self.name);

let _left = table.forks[self.left].lock().unwrap();
let _right = table.forks[self.right].lock().unwrap();

println!("{} is eating.", self.name);

thread::sleep_ms(1000);

let _right = table.forks[self.right].lock().unwrap();

println!("{} is done eating.", self.name);
}
}
Expand Down Expand Up @@ -596,13 +595,12 @@ We now need to construct those `left` and `right` values, so we add them to

```rust,ignore
fn eat(&self, table: &Table) {
println!("{} is about to eat.", self.name);

let _left = table.forks[self.left].lock().unwrap();
let _right = table.forks[self.right].lock().unwrap();

println!("{} is eating.", self.name);

thread::sleep_ms(1000);

let _right = table.forks[self.right].lock().unwrap();

println!("{} is done eating.", self.name);
}
```
Expand Down Expand Up @@ -660,7 +658,9 @@ We need to pass in our `left` and `right` values to the constructors for our
you look at the pattern, it’s all consistent until the very end. Monsieur
Foucault should have `4, 0` as arguments, but instead, has `0, 4`. This is what
prevents deadlock, actually: one of our philosophers is left handed! This is
one way to solve the problem, and in my opinion, it’s the simplest.
one way to solve the problem, and in my opinion, it’s the simplest. If you
switched these around and caused a deadlock you can use `Ctrl-C` to interrupt
execution of the program.

```rust,ignore
let handles: Vec<_> = philosophers.into_iter().map(|p| {
Expand All @@ -686,14 +686,14 @@ With this, our program works! Only two philosophers can eat at any one time,
and so you’ll get some output like this:

```text
Gilles Deleuze is eating.
Emma Goldman is eating.
Gilles Deleuze is about to eat.
Emma Goldman is about to eat.
Emma Goldman is done eating.
Gilles Deleuze is done eating.
Judith Butler is eating.
Karl Marx is eating.
Judith Butler is about to eat.
Karl Marx is about to eat.
Judith Butler is done eating.
Michel Foucault is eating.
Michel Foucault is about to eat.
Karl Marx is done eating.
Michel Foucault is done eating.
```
Expand Down