My favorite analogy for locking is a lift in a building.

You have people (threads) moving around independantly on different floors (memory spaces) doing their alloted tasks. Occasionally, they need to share some information with collegues elsewhere and to do that they need to make use of a lift (shared resource). So, they move to the lift and press the button to signal their desire to use it.

If the lift is currently idle ( not signalled), then it responds immediately to their signal and the can move onto their destination (pretty much) immediately. If however, the lift is currently in use, they will have to wait for it to become free.

And there may already be several other people waiting (having signalled) for the lift, and which one will get serviced first is down to some permutation of their floor relative to the lift at the time they signalled; and at the time it becomes free; and who else is closer to the lift when it becomes free; and a bunch of other hueristics to do with scheduling (the OS dispatcher).

Of course there can be more than one lift (shared resource), and each lift can accomodate more than one person (shared arrays or hashes). And you can extend the analogy to deal with things like process inversions and deadlocks and similar. But, as analogies have a tendancy to do, the more you extend it, the more imperfect the it becomes. It's still a useful visualisation though.

The main thing to know about shared vars and locking in Perl, is that because variables have to be explicitly shared, the need for shared variables (and therefore locking, and the problems traditionally associated with it) is rare. For many applications, the user can avoid most, and even all need for explicit user sharing & locking by the juducious use of Thread::Queue.

And if you find yourself needing to employ widespread sharing and locking, then you are probably doing it wrong.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Threading in perl - using Locks by BrowserUk
in thread Threading in perl - using Locks by koti688

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.