Python, music, sports, gaming and philosophy.

Main menu

Post navigation

Anal About Analogies and Concurrency

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

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.

3 thoughts on “Anal About Analogies and Concurrency”

I’ve not seen the term “lock” used in programming as you describe. Does Python do so?

Usually a lock is something which is placed upon a mutex. That is, you lock the mutex, preventing anybody else from locking the same mutex. Waiters are not waiting on the lock, they are waiting on the ability to lock the mutex.

IIUC mutex and lock are the same thing. In windows programming a mutex is an OS provided lock which can be shared between processes and a critical-section/spin-lock is only available in the current process. I defer to wikipedia’s judgement: http://en.wikipedia.org/wiki/Lock_(computer_science)