views:

625

answers:

7

It seems that often it is better to use real-life analogies to explain some things. But I can't find a single all-encompassing analogy to talk about all things multithreaded. Sometimes I use the room and the locking door analogy, but it doesn't explain deadlocking very easily. The flashcards and TV-watching paradigms are even more limited in number of scenarios they cover...

Have you found a good analogy for this area? If so, what is it?

Edit: I'd like to be able to explain deadlocking AND race conditions AND high contention scenarios AND thread affinity using the same analogy, if possible

Yes, I agree that it's better when it's real. In other words I agree that it's better to avoid analogies for important problems like this. Unfortunately, life doesn't work this way and sometimes developers with little or no multithreading experience are contributing to parts of the system where the above mentioned aspects become critical and quick path to mutual understanding is required. Hence is the question.

Thank you very much for your input!

+2  A: 

Try patting your head and rubbing your stomach at the same time.

Hard isn't it.

Threading's harder.

Simon P Stevens
I find this extremely trivial actually... is there supposed to be some kind of mental deadlock?
Jimmy
+3  A: 

There are a lot of "real world examples" on the text books. If I recall correctly the Tanenbaum has a lot of them. From what I can recall the Dining Philosophers explained deadlocking pretty well.

Edit: It isn't the Tanenbaum (I've checked)... maybe it was the Stallings, I can't remember)

From the same all mighty wikipedia:

Explaining race conditions:

In many work environments, it is often difficult for software developers or engineers to explain a race condition to managers, who may not be familiar with technical details. In this case, a horse race metaphor example can be provided:

Example: A horse race represents the computer application. Each horse represents a specific element in the application, that are all running in parallel, such as user interface graphics versus network communications running concurrently. The software may require that a specific horse must win the race in order for the application to run properly. The application may work flawlessly if horse #5 finishes first, while the application crashes if another horse finishes the race before horse #5. One possible solution to this problem, is to add synchronization to guarantee that horse #5 finishes before #2. Metaphorically, this is similar to jockey #2 and #5 teaming up to let #5 run ahead of #2.

Sleeping barber problem

Jorge Córdoba
+1 I have never heard of the dining philosohpers problem until now. I always liked explaining the advantage of SQL Server vs Access databases(and the issues that arrise) by explaining the chaos of a buffet with a hundred people trying to grab food at once(Access DB) vs a system where a waiter/server comes out and serves everyone in an orderly fashion(which makes the analogy easy to remember because the term Server is used in SQL Server as well as the analogy). I have a feeling that is the concept behind why it is called a server so I probably can't take credit for that.
AaronLS
I love Dining Philosophers. I had to write an implementation in Java and for the pictures of philosophers, iirc, I used Plato, Socrates, Confucius and Bill and Ted from Bill and Ted's Excellent Adventure.
docgnome
aaronls Except by the fact that it is just the contrary. SQL Server is like a free buffet with a great great distribution. Everyone can pick whatever they want whenever they want with almost no problems. In access lock politics are more sever so sometimes you feel like you are being served by one waiter only and, even if no body is asking for the same data as you are, you have to wait. Oracle is almost total freedom :P You never have to wait
Jorge Córdoba
+1  A: 

I wouldn't really go with analogies. They tend to confuse people, without explaining the real problem; or even worse, give people the impression they know about the domain, when they actually dont :)

If you really want to go with analogies, the good old deposit/withdrawal story is fine for race-conditions. For deadlocks, assume 2 persons who can only do 1 thing at a time. Now, at one point person A waits for person B to finish, while person B waits for person A to finish => stalemate :)

cwap
That's why I like to preface things like that with, "This isn't completely technically accurate, and there's alot more to it, but this will give you the jist of the idea without having to get a computer science degree."
AaronLS
would say that, if you are writing low level locking or locking protocols then you either should have a CS degree or be entirely capable of getting one yourself in your spare time (if you had any). If a company has someone doing this who isn't up to it they are into a world of pain.
ShuggyCoUk
+1  A: 

Imagine a chicken, a goat, and a bobcat are all tied up in a burlap sack and then tossed into a river...

Avdi
The crocodiles don't have particularly good wait algorithms for when another crocodile has posession of the sack. You'd mostly likely get corrupted data because eats are not atomic.
Jimmy
This is the perfect analogy for the software I have to maintain.
Kristopher Johnson
+1  A: 

There is an intersection near my house that I've seen get into a deadlock state. A line of cars in one direction gets backed up trying to make a left turn but they can't make progress because they are blocked by another line of cars in the opposite direction also trying to make left turns. Every time I get stuck in that I think about deadlock and I'm the only person laughing about it while everyone else is quickly getting frustrated.

Brian Ensink
+8  A: 

Byzantine Generals are very in vogue and can be easily extended to rendition protocols for more complex issues.

Dining philosophers are old favourites but sadly do not cover such a broad scope now they have been dumbed down with TV dinners.

Cigarette Smokers are no longer appropriate, in fact there are moves to make the whole analogy illegal within 50 meters of a food service establishment which does make discussing it over lunch problematic.

Atomic isn't any good anymore, people just start talking about quantums, cat's in boxes alive or deadlocked until you open the box and before you know it it's two entangled phases and you've no idea which one you commit but the docs say either way your server's wave functions going to collapse.

ShuggyCoUk
ahem.... sorry :)
ShuggyCoUk
+1 for the links, -1 for the gags :-)
Andrew Shepherd
my link-fu is certainly better than my joke-fu. I feel guilty being at the top of the vote stack! perhaps I should merge my comments into the answer but don't want to imply that the votes represent assent with my opinions...
ShuggyCoUk
+5  A: 

My favorite real-world analogy for Threading is Traffic. Everyone understands it and threading (and especially threading problems) map onto it very easily and simply:

  • Simultaneity is obvious: multiple vehicles driving at the same time
  • Blocking is also obvious: single-lane road with a vehicle in your way (can also be done with filled parking spots)
  • Deadlocks -> gridlocks
  • External/imposed locks: traffic lights, stop signs (4-way stops signs are great)
  • Priority: emergency vehicles
  • Queuing: duh.
  • Stacks: parking lots w/ attendants (watch how they pack the cars in)
  • Programming with threads: A fleet of delivery trucks in a local municipal area, with new packages to deliver all the time.
RBarryYoung