Analogies are tools, they can add intuition, fun and functional value to thoughts and concepts. Here are a few:
- Learning quadratic equations is more fun when thinking of balls and rockets flying around.
- Assembly can be thought of as an analogy for turing machines, C can be considered analogous to assembly.
- Regular languages and finite automatons are equivalent, yet I personally would prefer solving an automaton problem over any regular language one.
“Lock” is frothy
The word “lock” isn’t the best at describing what it does for concurrency. From simple wikipedia:
A lock is a fastening device: a thing which keeps people from opening something, such as a door or a box.
But in concurrency locks don’t just prevent access, most of the time they cause whoever touches the lock to just wait there until the lock is “open”. Locks in the real world mean “you can’t have this” while locks in CS mean “hold on a second, I’m busy here”.
A better analogy for locks – the waiting room
Every book on the planet uses the example of a waiting room to explain locks. Why don’t we just call them waiting rooms? Here it goes:
Let’s say the resource we’re protecting is a doctor which wouldn’t be able to help 10 people (threads) at the same time because humans are terrible multitaskers. We invented the waiting room with a secretary (aka the operating system) that gives each person a ticket with some random number on it and when it’s your turn, she calls out your number and hands you the key to the room with the doctor in it. Now that you have the key, be careful with it, you can open the door but if you have a heart attack before you return it – no one else will be able to access the doctor and all the poor saps in the waiting room will be stuck there for all eternity.
Table of analogies
|Lock||A waiting room with one locked door|
|Semaphore||A waiting room with multiple locked doors|
|Manual Reset Event||A waiting room with a big sign that tells everybody whether to wait or go|
|Deadlock||Two dumbasses are each waiting for a key held by the other, you might expect the secretary to intervene but she’s not that bright either.|
The fun part – what can we do with these
Now that we have better names/analogies, designing a concurrent system becomes an interior architecture problem. Build rooms and hallways to solve your computer problems. Check out this design for a Readers-Writer-Lock and imagine people walking around from room to room through the arrows. Try and figure out where people might get stuck or interfere with one another.