Closed zmaril closed 9 years ago
By introducing a small pause between reaching for the forks I was able to introduce a deadlock.
fn eat(&self, table: &Table){
println!("{} is reaching for the {} fork",self.name,self.left);
let _left = table.forks[self.left].lock().unwrap();
println!("{} got the {} fork!",self.name, self.left);
println!("{} pauses to compose themselves.", self.name);
thread::sleep_ms(100);
println!("{} is reaching for the {} fork",self.name,self.right);
let _right = table.forks[self.right].lock().unwrap();
println!("{} got the {} fork!", self.name,self.right);
println!("{} got the forks! Time to eat.", self.name);
thread::sleep_ms(1000);
println!("{} is done eating.", self.name);
}
Knowing that I can write Rust deadlocks and then see how reversing the arguments fixes the deadlock helps me understand what Rust can and cannot do. Would folks be interested in a pull request expanding the Dining Philosophers page to include example that actually deadlocks?
So, it introduces the possibility of deadlock, it doesn't guarantee that it will. As you've noticed. :)
We already have https://github.com/rust-lang/rust/issues/27932 open, and it hasn't been dealt with, so I'm going to give this ticket a close, as it's effectively a dup. I would take a PR with your explanation, as it would fix that issue too :)
Is it possible to compile code that deadlocks in Rust? I'm trying to break the Dining Philosophers code and get it to deadlock but I can't seem to get it to freeze up. I've tried switching the forks of the last philosopher to
4,0
instead but it still all finishes. I know this is a storied problem with a long history and I'd like to see/write a partial solution and see what it looks like in Rust. However, I don't know if Rust is smart enough to stop me and is somehow mitigating all the deadlocks I am introducing.