Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Threading in perl - using Locks

by koti688 (Sexton)
on Nov 21, 2008 at 11:02 UTC ( [id://725097]=perlquestion: print w/replies, xml ) Need Help??

koti688 has asked for the wisdom of the Perl Monks concerning the following question:

can you please explain me the lock mechanism how it works exactly.

i am unable to get clearly and follow in this link. itz confusing for me..


http://perldoc.perl.org/perlthrtut.html



Please explain with a small example.
if anyone knows , any thoughts about the basics of working with locks will be helpful for me.
Thanks - Koti

Replies are listed 'Best First'.
Re: Threading in perl - using Locks
by moritz (Cardinal) on Nov 21, 2008 at 11:15 UTC
Re: Threading in perl - using Locks
by Wiggins (Hermit) on Nov 21, 2008 at 14:07 UTC
    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:

    • Number of emails processed,
    • Number of bytes processed.
    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 .....
Re: Threading in perl - using Locks
by BrowserUk (Patriarch) on Nov 21, 2008 at 14:55 UTC

    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.
Re: Threading in perl - using Locks
by zentara (Archbishop) on Nov 21, 2008 at 13:50 UTC
    I know this is a copout, but I find that I can write threaded apps, and avoid the locking problem, by having a larger set of shared variables, where each thread has it's own var for writing to, and other threads only read from it, so there is no simultaneous writes. Of course, there are situations where you need locking, but I just want to point out you can design around it.

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://725097]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-16 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found