The dining philosophers problem
We visited the dining philosopher’s problem in Chapter 1, Concurrency: A High-Level Overview, while discussing concurrency at a higher level. This is an important problem in the study of critical sections. The problem may seem contrived, but it shows a problem that comes up often in real-world situations: entering the critical section may require the acquisition of multiple resources (mutexes). Any time you have a critical section that relies on multiple mutexes, you have a chance of deadlock and starvation.
Now, we will study some solutions to this problem in Go. We will begin by restating the problem:
There are five philosophers dining together at the same round table. There are five plates, one in front of each philosopher, and one fork between each plate, five forks total. The dish they are eating requires them to use both forks, one on their left side and the other on their right side. Each philosopher thinks for a random interval and...