In 2014-2015 I fooled around with concurrency in C++ a lot, mainly to learn
about software transactional memory and lock free data structures. I learned
a lot but never quite got to polishing anything up. Since I have code lying
around and tons of free time to waste, I’m tidying it up, writing about it,
and putting it up. Particularly with STM on gcc, there’s not much info around
it seems. That’s sad, because it’s such a cool feature! I’m not sure I’d use
it in a pacemaker or anything, but for hobby projects or scientific
applications I think the idea has great merit and is completely useful today.
In the previous post, we looked a some advantages
of STM in the context of a bank account transaction. Let’s tackle a famous
problem having to do with concurrent systems, the
Dining Philosopher’s Problem.
Recall that in this problem a group of philosophers sit at a round table.
Between each of them is a fork. In order to eat their meal, the philosophers
need both forks, so each tries to grab one fork and then the other. If they can
do so, they eat for a bit and put both forks down.
When I was a kid, if you were lucky enough to have a computer, it had one core
and not much memory and it ran really slowly. It’s a wonder anything got done.
We couldn’t even order toilet paper from Amazon because Amazon didn’t exist
yet. But now, lots of cores are the norm, and things are only going to get
better (worse?). How can we deal with the additional complexity brought on by
the need to use these cores effectively?