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.

  • $\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

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.

  • $\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

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

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

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.

  • $\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

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.


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.