0

Ok so I am trying to solve the Dining Philosophers problem. I have only one question. I have a class Philosopher and another class Chopstick. I want that the class Philosopher to have 5 global Chopsticks. By this I mean that each instance of the class Philosopher has the same 5 Chopsticks: if any instance modifies a chopstick, then this modification should be visible to the others 4 as well. One solution for this would be to nest classes, but this is something I would like to avoid. Any ideas ?

A second idea I had has to do with references but I think I am wrong. I was thinking of declaring in the main of my program ( Philosopher is not my main class ) 5 Chopsticks ( or a Chopstick Array, doesn't matter). Then call 5 times the constructor of Philosopher in order to create 5 philosophers. The Philosopher constructor would have a Chopstick Array parameter or 5 Chopsticks parameters and initializes the 5 Chopsticks of each philosopher. Are now the chopsticks of each philosophers linked to the same chopsticks, and hence global ? I hope I explained my problem well enough.

0 Answers0