# Wishful Coding

Didn't you ever wish your computer understood you?

## Dining Philosophers in core.async

For the July Amsterdam Clojure meetup, a lot of people where curious what core.async is all about, so I proposed tackling the dining philosophers problem with core.async.

The problem is explained and a solution proposed in the CSP book section 2.5, but using events rather than channels.

We worked on the problem dojo style, switching the driver seat every few minutes. But with no one really knowing the library very well, progress was slow, and by the end of the meetup we could make one philosopher eat.

One problem we ran into was that go blocks are lexical, so you can’t easily write helper functions that use <!

So this morning I sat down to make this thing work.

During the meetup we had a function that would do something silly to setup a vector with 5 forks represented by channels, which I replaced by some equally silly, until I just came up with this.

I use channels with a buffer of one and put a fork in that buffer. This makes sure a fork can but picked up and put down without blocking, but only once.

The butler making sure only 4 philosophers are seated is simply represented as

This leads to the definition of the basic actions

The simplified behaviour of a philosopher then becomes

And finally we can run 5 philos threads.

Be sure to check out the code and output and join the next meetup if you’re in the Netherlands.