I'ver never used locking in any of my threaded scripts,

But, if I recall correctly, you do not use a multi-core processor. Running threads on a single-core processor -- where only one thread will ever actually be running at any given moment in time -- means that you can get away with a great many dubious practices that will come back to bite you in the ass when you move the code to a multi-score processor -- where 2 or more threads may be running concurrently on different cores.

Also, you have almost certainly used elective locking without being aware of it. Thread::Queue uses locking to ensure safety. Indeed, often the simplest way to get a threaded application running is to use that module to start, and then revert to using a shared array (which is all a Queue is underneath) and perform selective locking to improve performance.

But I will quote his fine print:

Indeed. As he pointed out. If you are inserting and deleting elements from a hash or array in one thread, and iterating it in another, the iterating (reading) thread needs to lock the hash otherwise it will get inconsistent results. Ie. the reader may iterate over elements that are subsequently removed (by another thread) before the iteration has completed, or it may miss newly inserted elements. If instantaneous consistency is important to the algorithm -- which is actually far rarer than you might think -- then the reader must lock the hash while the iteration is in progress.

But the need for locking is certainly not "`anytime'"; nor "dictated"; nor "comprehensive".

Prosaically, safe practice is to lock everything, everytime. But the downside is a substantial hit on performance. In almost all cases it is not just possible, but advisable to relax locking where it can be reasoned (or demonstrated) to be unnecessary.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

In reply to Re^4: threads::shared - when to lock a hash ? by BrowserUk
in thread threads::shared - when to lock a hash ? by Anonymous Monk

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.