A brief, yet helpful, lesson on elementary resource-locking strategy

Last night, Fiona and I sat down to eat my favourite light meal, spaghetti puttanesca.  I’d grated some cheese to sprinkle on top, but when I’d finished mixing the pasta in with the sauce I found that the cheese had GONE!

Fiona had taken it down to the other end of the table, next to her plate.  I asked if she’d finished with it, and it turned out that she HADN’T EVEN STARTED.  She’d just acquired the lock, then left the resource unavailable while she responded to a completely unrelated event.

I explained to her that it’s an elementary principle of concurrent system design that you minimise your lock phases: acquire a resource, use it and release it as quickly as possible, so that the resource can be used by other agents.  But she gave me some dumb story about having had to get up to fetch drinks for the boys.  Then she said that I could have had the cheese back more quickly if I’d got up to help!

I explained as politely as I could that separation of concerns is one of the most fundamental of all the principles of system design, and that for me to reschedule my own tasks and take on other agents’ responsibilities would be a gross violation of encapsulation.  I explained that, instead, when she accepted the get-the-boys’-drinks interrupt, she should have relinquished her lock and passed the cheese back down to my end of the table before going swanning off off on the drinks mission.

Sadly, she was COMPLETELY IRRATIONAL and started talking as though I was some kind of selfish jerk who just wanted the cheese.

I tried to be sympathetic, but the bottom line is that she was wasting a valuable opportunity to BENEFIT FROM MY EXPERTISE and learn important architectural principles.  I just don’t understand women.

[By the way, I noticed yesterday that The Reinvigorated Programmer is now just over a month old.  Many thanks to all of you who have read, and especially commented on, these posts — you’re making this site what it is.  I hope you’re very proud.]

Update (later the same day)

Lots of discussion at Reddit, but this comment by egarland is sheer gold — I LOLled, literally.  Amazingly, it seems this “Lite” post is top of the Reddit front page right now.

94 responses to “A brief, yet helpful, lesson on elementary resource-locking strategy

  1. In the multi-core processor age, to have only ONE cup of cheese can be a real bottleneck…

  2. You should have grated your cheese onto two plates, mate.

    This would be like multithreading.

  3. A classical example of multi-threaded starvation.

    I just hope you are still coupled with the other agent.

  4. Pingback: Sarcastic Developer » Blog Archive » Grated cheese is not thread safe.

  5. Hilarious!! Exactly the thoughts that go through my head in real life.

  6. One of the best IT blogs I’ve ever read. Keep up the good work ;)

  7. Findlers McGiven

    Fabulous. I’ve had a crappy day but that’s just made me smile :)

  8. I’m always surprised with how little system design knowledge people expect to get through life :)

    A (technically) acceptable solution would have been preemption: time slots for the cheese. I’d be interested in a lessons learned posting after implementation :)

  9. I’m just glad you didn’t have a deadlock because you were holding the grater and she had the cheese.

  10. You have summed up the difference between the sexes in one hilarious blog post.

  11. Pingback: cleek » A brief, yet helpful, lesson on elementary resource-locking strategy

  12. ha ha. awesome.

    maybe you can write some cool script that will totally get this agent under control. or get you killed :-)

  13. You failed to realize that two little viruses have entered your system and will attempt to consume all the available resources regardless of priority or purpose. She is not irrational, she’s just been reprogrammed by the viruses.

  14. Carlos Azevedo

    That situation sounds like a Dilbert comic strip.

  15. dyin’ over here…

  16. This is why we have to cheese grinders. We can’t get stuck behind the other. Our kitchen has vast storage capacity so we just throw more resources at the problem. No need for optimization.

  17. Win! Wow that was funny.

  18. hahah. no fighting over the last black olive!

  19. That was funny. The multithreading comment was the best.

    Rafael

  20. YES! Reinvogorated me for sure. In similar fashion I have attempted to explain the efficiency of using the kitchen sink as a dirty dish buffer. The distance from the water heater to the sink is enough that warming the pipes is a significant energy investment, not to mention that the interrupt priority of such tasks is significantly lower than servicing my dinner partners post-prandial demands.

  21. I think you underestimate your wife. Clearly all of your processes were waiting on that lock. Had your own system been designed more like your wifes, you could have engaged in a creative disentanglement of resources, offering your own (currently unused) processing capabilities to any process that claimed priority on the locked resource you needed. For instance, had your wife said “I cannot release the lock on the only current instance of the cheese object until I run Kitchen.fetch(drinks, boys)” You could have run your own fetch routine, possibly at the same time as hers, resulting in the boys getting two resulting drink sets and you getting your cheese lock back while in the middle of that process. Or you could have gone to the kitchen and gotten a second cheese plate, created your own instance of the object she had a lock on, and completed your dinner long before she even had a chance to start on hers, freeing you up to process the garbage collection routine on all dinner objects. And by that I mean do the washing up.

  22. Max, you have a point; but I think that at this stage the boys are old enough that we should be invoking:

    %w(dan matt jonno).each { |boy| boy.get_your_own_darned_drink }

    rather than the legacy implementation:

    %w(dan matt jonno).each { |boy| mum.get_drink_for(boy) }

    Bonus observation: the new algorithm also works in lame languages without closures.

  23. Women are still living in single tasking operating system (probably still on DOS) :-)

  24. OK, firstly, I’m NOT irrational.
    Secondly, Mike should just get over it, already.
    Thirdly, what on earth is multi-threading?
    Fourthly, we need to talk.
    (Posted by Mike’s wife).

  25. I’d have solved the problem like I do with all the other problems I have – by grating the cheese right onto everyone’s dinner – simple, saves washing up. You grate the same amount of cheese – less in fact, because you only grate the right amount, you don’t feel obligated to eat the left over cheese rather than waste a sandwich bag to preserve it or throw it out. It’s also a pre-emptively strike against the wife holding the cheese hostage to lecture you about not following the principle of ‘many hands makes light work’.

  26. Max, you will never understand that you HAVE to acquire a lock regardless of your needs, just because it’s on sale. Where do you gonna sleep tonight? On the couch??

  27. Glad you made it to the front page. In fact it is quite a nice and funny post what you made. Congrats!

  28. Wait, BenAlabaster — you solve ALL your problems by grating cheese right onto everyone’s dinner? Does that work? It’s am amazing breakthrough if it does. We need to get you involved in the Middle-East peace process, stat. Also, maybe you could sort out the USA’s heath-care issues. By, you know, grating cheese directly onto everyone’s dinner. I mean, it’s gotta help, right?

  29. Nathan Lasseter

    Gee, I do love a good critical section.
    Maybe you could have used the Bankers algorithm to determine a safe way to allocate the resource?
    =]

  30. puttanesca should never be served with cheese. It has fish in it.

  31. haha!

  32. To Fiona (Mike’s wife),

    Just because he SAYS you are irrational doesn’t mean you are. He’s just not privvy to all aspects of your software. Like the untrained user who thinks his computer is acting crazy, but to the IT guy who completely understands it it’s completely logical.

    Mike’s over it. He’s just having fun now.

    You *DO* know what multi-threading is, but maybe not by that name. With two young boys you do more of it every day than he could hope to do in a lifetime.

    Talking probably won’t help. ;)

  33. Pingback: The Boschmans Account » Blog Archive » Understanding Resource Locking…

  34. brother you are preaching to the choir. Ever since I was a child, I have been irritated by the actions of my sister, who would have the butter in front of her when I ask her to pass it to me. Instead of passing it to me in a polite, timely fashion, she would make me wait until she had finished what ever she was doing, served herself butter, then pass it to me.

    All the while I sit waiting for her to get done with her own singular processes and am allowed to finally serve myself butter.

  35. Sounds like you need better fine-grated locking

  36. Unlock this situation by attaining and pursuing to be inebriated by some grape fermented liquid derived from the hillsides of Le France to enhance this cheese dilemma your currently experiencing. Studies show, that this quick commercialized remedy shall render the female homo-sapient from any further inhibitions to comply with your conjectures on this web page of your personal needs to acquire some cheese. Speed is of the essence mon frere… furthermore, zee super fantastic mega-blockbuster action-hero Arnold Schwarzenegger just interjected with an infamous quote of his, “I’m here! Do it NOW!!!”

  37. You sound like a couple of dining philosophers!

  38. Seth Hodgson

    Cheese Convoys!!!

  39. unfortunately in today’s world being a programmer is an excuse to act like a Dick.
    good people make concessions and
    Dicks Are Dicks.

  40. Stacey Becker

    For Fiona (and other wives in the wonderful world of IT.)

    http://hubpages.com/hub/What-to-Expect-When-You-Marry-an-IT-Nerd

  41. @ Joseph Le Brech
    If he had grated cheese onto two plates that wouldn’t have been multithreading, it would have been multiSHREDDING…
    ::hides from the pun retaliation::

  42. Pingback: Top Posts — WordPress.com

  43. I’ve seen this type of problem a lot in spaghetti code.

    -fred

  44. Pingback: Interesting Reading #455 – China preparing to bomb aircraft carriers, Amazing Boeing wing test, Fatty foods may be addictive, best free software, Nvidia’s problem and much more… – The Blogs at HowStuffWorks

  45. cough..cough…semaphore…cough

  46. I believe use of two plates would be an inefficient use of resources, leading to increased burden on cleanup routines and the garbage collector.

    Perhaps a shared lock may have more efficient. It might have meant moving the two competing processes into the same session to help with inter-process communication, although I would agree this could lead to memory (table) contention, and a potential deadlock. Yike.

  47. If I had tried that explanation on my wife (who does understands systems engineering, yet also has to get drinks for our boys), she woulda clocked me, and it would have megahurtzd.

  48. I think reading this actually made me a little smarter. Can anyone use the aquiring a lock concept in a more programming related example to help me understand it a bit better?

  49. Does the processing speed of your meals double every 18 months or so?

    LOAD”*”,8,1

  50. Your complimentary marital unit is operating at less than operational reliability.

    My guess is that she got sick of your geek-speak.

    There is a another possibility, which is she does not care about you, i.e. does not love you. In which case you might as well get divorced.

  51. Hilarious! My twisted sense of humour considers this the absolute best post on this blog yet. More like this!

  52. You should have just grabbed the cheese and used it hoping she wouldn’t look back. Locks only slow you down.

  53. Pingback: Steve Wills’ Weblog » Blog Archive » Twitter Updates for 2010-03-31

  54. Pingback: Elementary Resource Locking explained via Dinner example « Ultra's Brain dump

  55. Don’t worry, k, your concerns are unfounded. We’ve been happily married for 17 years; there mere fact that she was COMPLETELY IRRATIONAL about some cheese and REFUSED TO LEARN what could have been a VALUABLE LESSON is not going to change that.

  56. Dining philosophers, locked by cheese!

  57. You forgot the rules engine! Women are always right, especially wives. Yes dear. And no, you don’t look fat in that. Even after the cheese.

  58. An excellent example.

  59. I hate it when my single threaded db client freezes because it thinks it has to wait for my multi-threaded MySQL Server to return a row set…

  60. It’s like a single threaded web browser freezing while Apache has to deal with child processes…

  61. Pingback: Chipping the web: March 31st -- Chip's Quips

  62. Thread 1 needs the following logic inserted in his code.

    IF
    lockholderThreadTimeToResourceUse > myTimeToSneakResourceAndPutBack

    THEN
    get lazy ass out of chair;
    walk to other end of table;
    grab cheese;
    walk to own plate;
    use cheese;
    walk back to thread2 plate;
    replace cheese;

    also

    IF marriage = good
    ++flexibility
    ELSE
    complain in a cute way with futile hope real emotions aren’t blindingly obvious through the cute

  63. Haha.. gud one.. where were u when i was learning how to program!

  64. LOL! What a “grate” story.

    I don’t know about the rest of you, but under the heading of “A happy wife means a happy life”, my wife has taught me that the most important things a husband can learn to say are:

    1. Yes, ma’am.
    2. I’m sorry, ma’am. I’ll never do it again.
    3. Right away, ma’am.

    I think you’re up for a #2. ;-)

  65. Fred, I am genuinely interested in what Real Emotions you think you detected. Do tell.

  66. Very funny and very memorable. You can bet hundreds of computer science profs will add this story to their lectures on locks.

  67. This is why I tell my guy friends they should stick to geek girls. They come with pre-optimized routines!

  68. Pingback: Geek humor… « Healthcare Systems Engineering and Analysis

  69. What you probably should have done is allocate cheese in thread local storage. This way, there is no contention, everyone has cheese, and nobody segfaults when the location of their cheese is modified by an errant thread.

  70. Pingback: Biweekly Links – 04-02-2010 « God, Your Book Is Great !!

  71. Pingback: Max’ Lesestoff zum Wochenende | PHP hates me - Der PHP Blog

  72. Smaller table….Or sit closer to her.. The only programming I ever did was with GFA on the Atari so can’t help you there

  73. Just read this post to my girlfriend… she came up with some very important questions you left unanswered…

    – Does your wife work? If not and she’s handling the household, you’re a jerk!
    – Did you cook or at least help your wife with it? If not, you’re a jerk!
    – How did she get the cheese in the first place? Did she have to grab it from your side of the table? If so, not only are you a jerk but you got the situation all wrong. That’s because then she’d implicitly released the lock on the resource by reacting to the interrupts and just left it in HER local cache where it was in yours before that so you’d just have to make the same effort to get it as she had to.

    Additionally: You said that you had the cheese resource locked while mixing the pasta with the sauce, which also is an unrelated task so your resource handling isn’t better in any way.

  74. Loved this post. Mom needs a rewrite, though. Interrupts need to be handled quickly. Just acknowledge the interrupt and schedule a deferred procedure call to get the drinks at a much lower priority level (or spawn a thread to let the boys get their own drinks). :)

  75. Pingback: Ed The Dev .com » Blog Archive » Cheese and Resource Locking

  76. Hey you could’ve cut the cheese slab into two halves. That way, you could’ve got the cheese + could’ve escaped that argument with your wife! Partition the resources! ;)

  77. Being a DBA I would have killed the offending deadlock, and thus taken control of the cheese.

  78. Daniel Taylor

    Don’t you think, the Problem is not so much Fiona’s admittedly terrible ability to efficiently use resorces as her husband’s inadaquite programming in the area conserning empathy, and flexability…

    Perhaps the old husband should be thrown out and a new, revised model installed?

  79. @Mike re: Fred: I’d be willing to bet those Emotions exist in both the Real and Complex planes. And I highly doubt they’re linearly separable in _any_ number of dimensions, so it may be that a neural network cannot find a solution.

    @Tatts: can semaphores exist for an operation that involves quantum states? How can you be sure you actually acquired the lock?

  80. Rational women would make a boring company. :D
    Good illustration of the principle though!

  81. Pingback: The Pandorica Opens (11th Doctor, episode 12) « The Reinvigorated Programmer

  82. Pingback: Things to Make and Do, part 6: fun with ostrich heads « Sauropod Vertebra Picture of the Week

  83. I’m with Ben, Italians would NEVER eat Puttenesca with cheese- but actually I know these guys & I know they won’t let a thing like that stop ’em.
    @luxifer- Mike does most of the cooking & they both work…

  84. @sarah b: good to hear… but still my third argument remains intact :-)

  85. Pingback: Locking Resources at Dinner « Jason Presley Dot Net

  86. Pingback: Two Guys Arguing

  87. Pingback: 2011.tech_resolutions.pop(5.questions) « Two Guys Arguing

  88. Frankly, all I can say is “Serves you right for putting cheese on a puttanesca.” It doesn’t need it and it doesn’t work: it’ll be drowned by the chilli. Go to Rome (the home of the puttanesca) and you’ll not be offered cheese with it – for a good reason.

  89. Ah, such dogmatism from one so young. I prefer to think of cheese on the puttanesca not as “wrong” but as fusion cuisine — as an experiment which in my experience, whatever you may say, does work.

    No doubt there are Japanese who feel that Californians are terribly wrong to put avocado in their sushi. But they’re not.

  90. There’s a little questioning on the home of puttanesca, someone say Rome, someone say Naples, but one for sure: avocado in sushi is a bright invention…

  91. I know you sooo well Mr Taylor!

  92. Hey! That’s “I know you sooo well Dr Taylor!”

  93. Hmmm, the word ‘pedant’ immediately springs to mind ;)

  94. What else would you expect from the originator of The Self-Appointed Grammar Police?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.