I will assume you have a handle on threads. Locking requires two concepts to be understood ... the reason for locks, and the mechanics of setting and clearing a lock

The reason for locking is to allow only 1 thread of control to manipulate a resource (or set of resources)at any given point in time. The mechanics are implementation specific.

Example... I have a Perl app that examines emails at my MTA. The examination takes longer than the interval between email arrivals, so I have multiple threads (each handling a single email). Think multiple checkout cashiers at a store, servicing one long customer line.
I have two counters:

which are updated by each of the threads at the end of processing an email.

This code example is of the mechanics of a single lock, there are many design patterns on how to us locks, when both updating and reading protected data. Imagine multiple threads updating the same linked list at the same time.

# update shared counters use threads; use threads::shared; # needed for shared variables my $TotalsLock:shared; # variable to be the lock my $MsgCnt:shared; my $TotalBytes:shared; .... { lock $TotalsLock; # I have locked access $MsgCnt++; $TotalBytes += $bytesThisemail; } # lock released at end of enclosing scope
Perl locks are also "discretionary" (like seat belts) in that you can access those shared variable in other placed without locking the access if you want, your choice. But you might get an updated MsgCnt, with an old TotalBytes.

And to print out the results:

# update shared counters use threads; use threads::shared; # needed for shared variables my $TotalsLock:shared; # variable to be the lock my $MsgCnt:shared; my $TotalBytes:shared; .... { lock $TotalsLock; # I have locked access print "Activity Totals: $MsgCnt $TotalBytes]n"; } # lock released at end of enclosing scope
Hope this helps .....

In reply to Re: Threading in perl - using Locks by Wiggins
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.