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 1 commit
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
22 changes: 11 additions & 11 deletions src/doc/trpl/dining-philosophers.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,12 @@ impl Philosopher {
}

fn eat(&self, table: &Table) {
let _left = table.forks[self.left].lock().unwrap();
let _right = table.forks[self.right].lock().unwrap();

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


let _left = table.forks[self.left].lock().unwrap();
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) {
let _left = table.forks[self.left].lock().unwrap();
let _right = table.forks[self.right].lock().unwrap();

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


let _left = table.forks[self.left].lock().unwrap();
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 round and caused a deadlock you can use `Ctrl-C` to interrupt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make this around, please?

execution of the program.

```rust,ignore
let handles: Vec<_> = philosophers.into_iter().map(|p| {
Expand Down