Today I'm started to learn Rust. So I came to https://doc.rust-lang.org/book/dining-philosophers.html
But the example won't work correct. The philosophers won't eat concurrent. I think there is an failure in the example, but I can't figure out what.
It depends on your computer, and how fast it is. This example is being removed from the book in the next versions because of too many of these kinds of questions (not that you're wrong to ask! Just that it's confusing)
Yeah I think you are right that they all eat in the same order. Because of the sleeps, I don't think computer speed effects the outcome, less it's a very very slow processor.
Because Michel is picking up the forks in reverse order, Emma is the first one who is able to grab both forks. When she's done, Karl is unblocked, then Gilles is unblock, then Judith is unblock, then Michel is unblocked.
I guess what I'd like to see is add a random delay before the p.eat(&table) in the range of 1 second to cause anything interesting to happen, that way they are coming to the table at an undetermined time.
Yeah that looks like what I was thinking. I'm assuming the example didn't do that because it uses an external crate and they wanted it to just use the standard library. However there is no random in the standard library so they're kind of stuck.
Whoa, I searched up this post for the exact same question, but I would certainly prefer the mild confusion over not seeing concurrency in the introduction at all. Isn't concurrency one of Rust's selling points anyway?
After reading the first two examples in the Rust book, I was thinking "so Rust has the same features as C++ and is more verbose; next language please".
I think the second example (dining philosophers) should present more interesting/unusual features. Maybe how OO and functional programming styles can work together.
Due to the way the program is structured, there are only 2 races:
a Judith/Michel race for Fork 0
an Emma/Michel race for Fork 4 if Michel one the Judith/Michel race
... leading to only 3 different orders:
Emma, Karl, Gilles, Judith, Michel (in case Judith wins Fork 0)
Emma, Michel, Karl, Gilles, Judith (in case Michel wins Fork 0 but loses Fork 4)
Michel, Emma, Karl, Gilles, Judith (in case Michel wins both Fork 0 and 4)
However, the execution tends to be very deterministic and unless one introduces random sleep times before the "left fork" lock one generally only sees a single order. And indeed, as the SO question mentioned, they do not eat concurrently.