8
$\begingroup$

TL;DR: How would you explain what is a race condition to a five years old. Which analogy to concepts he is familiar with would you use?

Long version: I am not a teacher but a software engineer. Yesterday evening, while discussing what the day was like with my five years old son I told him that I worked on an annoying bug, that ended up being caused by a race condition. He then asked me what a race condition is.

I tried to explain the concept to him by making the analogy with multiple people all writing a story on the same blackboard. If they all write their story at the same time then one person might start writing over part of a story already written by someone else.

However I think that this analogy was probably not the best because he then asked me which person did “won”. I explained to him that nobody did win because the end result was that the “story” that was written on the blackboard was not making any sense.

He then asked me how did I solved the problem and I told him that I did give each story writer his own blackboard.

So I am looking for a good way to explain what is a race condition to a five years old, ideally without using a programming terminology because that would mean nothing to him.

$\endgroup$
  • $\begingroup$ Just tell them that it is caused by shared mutable state. The pain of doing this will help you to re-consider your design. $\endgroup$ – ctrl-alt-delor Oct 20 '18 at 23:49
8
$\begingroup$

A lot of race conditions result from improper or unexpected interleaving of processes. Someone that age can probably understand the fable of the Turtle and the Hare.

Give the Hare a ball with instructions to run to a table, some distance away, and leave the ball on the table and return.

Tell the Turtle to go to the table, pick up the ball, and return with it.

Start the two "running" at the same time.

The Turtle has no instructions about waiting, since the proper interleaving is obvious.

Of course, if the Turtle gets to the table first then the process fails.


I'll note that Dining Philosophers isn't a lot more difficult than the above, but probably not accessible to a five year old. Certainly open to one a few years older, though.

$\endgroup$
  • $\begingroup$ How would you modify this to deal with the "sometimes" fails for race conditions? $\endgroup$ – Gypsy Spellweaver Oct 4 '18 at 19:11
  • $\begingroup$ @GypsySpellweaver, well, sometimes the bunny does, in fact, do the right thing. And sometimes the turtle is the one who wants a nap. $\endgroup$ – Buffy Oct 4 '18 at 19:12
  • $\begingroup$ I find the Turtle and Hare comparison really nice as it nicely illustrate that one actor might be “faster” than another one but might take “pauses”. About the Dining Philosophers I would definitely use it to illustrate what a deadlock might be, however I do think that the associated concepts might be a bit difficult to explain to a kid of that age (who would need two forks to eat??? ;) ) $\endgroup$ – Renaud M. Oct 5 '18 at 17:57
  • 1
    $\begingroup$ @RenaudM., the dining philosophers problem makes more sense if the table is set with chopsticks instead of forks. $\endgroup$ – Solomon Slow Oct 8 '18 at 18:38
  • $\begingroup$ 5 year olds love the stacking hands game, where the players interleave their hands in a stack, then each player when his hand is one the bottom of the stack pulls it out and places it on the top of the stack. The players get faster and faster until there is just crazy slapping as everyone's hands collide. That is a race condition. $\endgroup$ – pojo-guy Oct 27 '18 at 2:32
4
$\begingroup$

Imagine two guys at a door. Each says, "No, you go first." Result: deadlock.

$\endgroup$
  • $\begingroup$ Simplicity itself. $\endgroup$ – Gypsy Spellweaver Oct 23 '18 at 1:01
2
$\begingroup$

Musical chairs where one person sits on another as they are both going for one of the last chairs — this is comical, though in seriousness, clearly a less than desirable seating arrangement.

$\endgroup$
  • $\begingroup$ Pretty close, except that "everyone sitting" is guaranteed to fail here. Instead, have a second "process" remove a chair after the music starts. If the music stops early, everyone sits, otherwise the usual thing. Even this is a bit suspect as one of the two processes (circling, removing) will always fail. $\endgroup$ – Buffy Oct 5 '18 at 10:58
2
$\begingroup$

When there is a slide in the playground where many children want to slide, they need to queue and each one needs to check if the slide is free (i.e. if the previous child got up from the bottom of the slide) before going down. If they don't check it, they may run into one another and hurt themselves.

That is what happened to your programs* - some where playing carelessly, and caused accidents.

Then you gave own slide to each of the programs, so that they can play safely, even if they want to slide simultaneously! enter image description here

You can use aquapark water slides instead if you think that the normal ones are not convincingly dangerous.

* Perhaps these where threads, but I don't think the distinction is important. Better to keep one term.

$\endgroup$
1
$\begingroup$

I would use the nursery tale of two goats across a narrow bridge. Once the first goat is on the bridge, the second one needs to wait before getting on the bridge till the first has crossed over, so as to avoid a collision. That is also why road workers use flags also called semaphores to coordinate traffic on lane closures.

New contributor
Kutluhan Erol is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
1
$\begingroup$

I think a case that hasn't been covered yet is the possibility of work being completed twice or work being lost due to a race condition. Here is an example to fill that gap:

You have 2 groups of dogs, the "carriers" and the "pick up-ers". They are on opposite sides of a field, and in the middle is a small cup, which only one ball can fit on top of. The carriers have a pile of tennis balls, and have been trained to bring the balls to the cup. The pick up-ers have been trained to take a ball out of the cup and bring it back to their side. You are watching this happen and counting the number of balls that have been brought over by counting the dogs-with-balls-in-their-mouths that reach the pick-up-ers side.

What happens if two carrier try and put a ball in the cup at the same time? One will fall on the ground, and won't get picked up, causing you to lose that tennis ball. Or what if two pick-up-ers try and grab the same ball and both grab it together? You'll count it as two, since two dogs-with-balls-in-their-mouths crossed the finish line, but actually only one ball has been carried.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.