This page looks best with JavaScript enabled

Feeding the Dining Philosophers - Async Programming in C# - Part 2

 ·   ·  ☕ 2 min read

    Returning once more to our dining philosophers and their ponderous feast. We left them starving having been deadlocked without the utility to eat. We’ll be solving this problem of deadlock in two different ways.

    The first will alter how one of our philosophers chooses their chopsticks. By doing this we ensure that at least one philosopher will always be able to eat. This solution was the one originally proposed by Dijkstra when he originally came up with this concurrent problem. It is the solution I recommend for this problem.

    The second solution introduces significantly more complexity to the problem by adding another member to our table. A waiter who chaperones the philosophers and serves to restrict who may attempt to retrieve their chopsticks. This allows each philosopher to safely ensure that both chopsticks are available before acquiring either of them and when they attempt to acquire the chopsticks that their not interrupted during the process. This solution is significantly more work and also posses some substantial risks to anyone attempting to maintain the code. As you’ll see, even a quick mistake can cause deadlock and therefore starvation to return back to the problem.

    Which do you prefer? The altered order or the waiter who oversees the chopsticks? Let me know in the comments!

    A solution which uses varied order to avoid deadlock:

    A solution created using the waiter method:

    Sam Wronski
    Sam Wronski
    Maker of things, currently helping build cloud things @ Microsoft. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!