Freigeben über


What my dog taught me about race conditions

I have a black Labrador named Sofie. She's about 2 and a half now, but still a puppy at heart. Here's a picture of her (along with her best friend, Sierra, who is a yellow lab):

One morning, Sofie reminded me of a valuable issue with threading and race conditions.  Our morning ritual for feeding is that Sofie starts dancing out of anticipation for breakfast (literally - fellow dog owners know what I mean), which usually involves her jumping up and down and chasing her tail in circles.
I then tell her to "sit" and then "stay", which means she's supposed to be stationary and not move until I say "OK". I then proceed to get her food, fill her dish, and wait a minute to reinforce her discipline. I then say "ok" and she rushes to her food dish and enjoys the life of a dog.

So what's the lesson?
One morning we came down, and instead of dancing, she just sat down and waited. So I proceeded to fill her dish ... and then she immediately ran and ate the food. I realized that because she was already sitting, I never issued the "sit" and "stay" commands.  To be more technical: Sofie and I each represent a thread, and her being in a "sitting" state lulled me into a false sense of security where I got lazy and avoided uses the synchronization primitives. Thus she was not ordered to wait and free to eat her food at her convenience.

Comments

  • Anonymous
    August 01, 2005
    Hehe. Thats a nice story. I would have just thought that your dog managed to circumvent the norm (its a hacker at heart) :-)
  • Anonymous
    August 01, 2005
    Think what might have happened if the thread had been running with administrative rights!
  • Anonymous
    August 01, 2005
    Майк Сталл — создатель компилятора C#, написанного на C# — рассказывает о пробле
  • Anonymous
    August 01, 2005
    Can somebody translate the comment that's not in english?
  • Anonymous
    August 01, 2005
    Long live Babelfish. :-)
    Here's its translation.

    "Mayk Stull [should be Mike Stall] - creator of compiler C #, written on C # - he tells about proble"

    Seems like its a trackback.
  • Anonymous
    August 02, 2005
    Sure it's a trackback:
    http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=c7cb2b25-47e5-4481-a0cf-5c0fefffc27d
  • Anonymous
    August 02, 2005
    By the way, Oleg Mihailik wrote that kind of your dog is the same as Russian President Vladimir Putin's dog.

    http://www2.chinadaily.com.cn/english/doc/2005-04/06/xin_080402061649742700616.jpg
  • Anonymous
    September 12, 2006
    My 19-month old daughter was quite the finite state machine today.
    She's recently become mobile and...
  • Anonymous
    June 07, 2007
    My wife + I recently had daughter #2. I'm taking time off, and so my participation on the forums and