I'm not a beginner at C# but I really need to increase my understanding, so I've picked a classic deadlock problem to code to help teach myself some of the more advanced concepts of C#. The Dining Philosophers Problem seems like a good one, but I need a little help to get started. I know I need to approach the "diners" as objects, but to simulate the random delays between eating, should I look to threading with each diner in a separate thread? Do I need some kind of "master" to monitor all the actions? Any general design concept advice is welcome, but I'd like to do the grunt programming as an exercise. Thanks!
I think the best approach to simulate it would be a Fork
class with a method like use()
that holds the fork (bool available = false
) and a release()
that releases it.
A Philosopher
class with getFork(Fork)
and releaseFork(Fork)
that operates the holding/releasing of the object Fork (seems to me a timer would be good in a method useFork()
so you can really perceive the deadlock.
And for Last a DinningTable
(or any other name) class that creates instances, and do the log. If you plan to use threads, here is where you should implement a thread for each Philosopher
concurring for the Fork
.
As a suggestion, you could implement a Plate
Class, holding a quantity of spaghetti that the Philosopher.useFork()
method lower during the time frame. This way you can see which Philosopher
finishes first.
I will let the implementations for you, of course, since your objective is to learn C# ... in my experience, you better learn doing something concrete like these classes ;) Besides, you can find lots of implementations on Google if you want to cheat ...
I invite you to share the code after it . It's a great Study Reference.
Hope This helps you.