Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Threads: why locking is required when using shared variables

by monarch (Priest)
on Oct 20, 2006 at 13:58 UTC ( [id://579585]=note: print w/replies, xml ) Need Help??


in reply to Threads: why locking is required when using shared variables

This is a great introduction and, certainly, locking of shared variables is essential learning and understanding for anyone who is going to be using threads.

Perhaps it is worth mentioning, in this particular case, that the lock function doesn't need an explicit unlock. Rather, a locked variable becomes unlocked when the code execution exits the current scope.

Replies are listed 'Best First'.
Re^2: Threads: why locking is required when using shared variables
by ikegami (Patriarch) on Oct 20, 2006 at 17:17 UTC

    That reminds me of a very useful bit of information reguarding locking. A single lock variable can be used to control access to multiple shared variables. For example,

    my $list_head : shared; # Access controlled by $list_head. my $list_tail : shared; # Access controlled by $list_head. sub ... { ... { lock($list_head); ... code that uses $list_head and/or $list_tail ... } ... }

    It doesn't matter which variable is used as to control access to a give shared variable, as long as you *always* use the same lock variable to control access to that shared variable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found