rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.21k stars 12.56k forks source link

TRPL Dining Philosophers, the last example code logic is incorrect #26368

Closed oakfire closed 9 years ago

oakfire commented 9 years ago

dining-philosophers I ran the last example code and I got:

Michel Foucault is eating. 
Michel Foucault is done eating.
Friedrich Nietzsche is eating.
Friedrich Nietzsche is done eating.
Karl Marx is eating. 
Karl Marx is done eating.
Gilles Deleuze is eating. 
Gilles Deleuze is done eating.
Baruch Spinoza is eating. 
Baruch Spinoza is done eating.

It is different from the book:

Gilles Deleuze is eating.
Friedrich Nietzsche is eating.
Friedrich Nietzsche is done eating.
Gilles Deleuze is done eating.
Baruch Spinoza is eating.
Karl Marx is eating.
Baruch Spinoza is done eating.
Michel Foucault is eating.
Karl Marx is done eating.
Michel Foucault is done eating.

So I add some code to find why:

    fn eat(&self, table: &Table) {     
        println!("{} want to eat. {} {}", self.name, self.left, self.right);
        let _left = table.forks[self.left].lock().unwrap();
        println!("{} got the left fork {}", self.name, self.left);
        let _right = table.forks[self.right].lock().unwrap();
        println!("{} got the right fork {}", self.name, self.right);

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

        thread::sleep_ms(1000);

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

and the result is

Michel Foucault want to eat. 0 4
Michel Foucault got the left fork 0
Michel Foucault got the right fork 4
Michel Foucault is eating. 
Friedrich Nietzsche want to eat. 3 4
Friedrich Nietzsche got the left fork 3
Karl Marx want to eat. 2 3
Karl Marx got the left fork 2
Gilles Deleuze want to eat. 1 2
Gilles Deleuze got the left fork 1
Baruch Spinoza want to eat. 0 1
Michel Foucault is done eating.
Friedrich Nietzsche got the right fork 4
Friedrich Nietzsche is eating. 
Baruch Spinoza got the left fork 0
Friedrich Nietzsche is done eating.
Karl Marx got the right fork 3
Karl Marx is eating. 
Karl Marx is done eating.
Gilles Deleuze got the right fork 2
Gilles Deleuze is eating. 
Gilles Deleuze is done eating.
Baruch Spinoza got the right fork 1
Baruch Spinoza is eating. 
Baruch Spinoza is done eating.

The book said:" Only two philosophers can eat at any one time,", that is not exactly correct.

Aatch commented 9 years ago

Your output is valid, but so is the example in the book. There can be two philosphers eating at the same time, for example, Spinoza (0, 1) and Marx (2, 3). Just because it doesn't happen in your case, doesn't mean it isn't possible.

I think this should probably be closed, but I'll let @steveklabnik decide.

oakfire commented 9 years ago

@Aatch Thank you, I am understand it is possible. But the difference confused me for a while, make me guess the 'mutex' worked in the abnormal state.

steveklabnik commented 9 years ago

Yes, this output is non-deterministic, so you won't necessarily get the same output.