Freigeben über


Implementing Dining Philosophers with the Agents Library

The latest issue of msdn magazine includes an article that I wrote which illustrates implementing the Dining Philosophers purely in message passing and without using any explicit locking. If you have Visual Studio 2010 Beta1 installed, you can also download the code and build the project, even if you don't you can browse the code online.

For folks that are curious about how locks are avoided, I'll state that the chopsticks themselves are messages and rather than the classic solution with semaphores, the philosopher uses the join message block to receive both messages from the table when it is time to pick them up. i.e. the code looks like this:

vector<Chopstick*> PickupChopsticks()

{

  //create the join

  join<Chopstick*,non_greedy> j(2);

  m_LeftChopstickProvider->link_target(&j);

  m_RightChopstickProvider->link_target(&j);

  //pickup the chopsticks

  return receive(j);

}

I'd encourage you to read the article and provide feedback either here or in our forums.

-Rick

Comments