3 Classic Examples
There are three classic examples in concurrency. Each represents a
fundamental problem that can be solved using threads.
They are...
This is a simple FIFO pipe between to tasks. The primary problem is
ensuring that the producer blocks if the FIFO is full, and the consumer
blocks if it is empty, and avoiding data-races along the way. A secondary
concern is that there is as little interference between the two tasks as
possible.
Here we have a number of threads which want to read from a buffer, and at
least one writer which needs to write to it. The biggest problem here is
avoiding starvation, although dead-lock can be a problem if you screw-up
the algorithm.
5 plates of spaghetti, 5 forks, and 5 hungry philosophers! The problem is
that each philosopher needs to use 2 forks to eat his
spaghetti. The problem here is contention for limited resources. The
danger is dead-lock. (in fact I had 5 different dead-lock bugs in the
process of writing this example :). Other concerns are starvation, and
avoiding excessive overhead when there isn't contention.
Well I hope you've found this useful. If there is interest I will add a
short section on dead-lock, starvation, and contention before the examples.
All constructive critisim is welcomed.
All flames > /dev/null. :)
<<<
Contents
>>>
Andrae Muys