for my $key ( @locking keys ) {
lock( $h{ $_ } );
## Presumably code goes here
cond_signal( $h{ $_ } );
}
Then I thought that maybe you want to unlock them all at once--but that isn't going to work because your signal loop is just as vulnerable to being interupted by the scheduler as any other peice of your code, which means that your other thread(s) are still likely to run and find one more of the keys unlocked and one or more of the other keys still locked.
If you do need to ensure that all the shared variables are unlocked before another thread goes ahead, then you should use a single guard variable for your locking:
#!/usr/local/perl_ithreads/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my %h = ( a => 1, b => 2, c => 3 );
my @locking_keys = qw/ a b /;
my $guard:shared;
{
lock( $guard ); ## Lock the guard
## do stuff with the hash
cond_signal( $guard ); ## unlock the guard (atomic)
}
You could also lock the hash itself.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
| [reply] [d/l] [select] |
I certainly considered using a single guard variable. The problem is that the routine will not know in advance which things will be locked. Further, I wish other threads to be able to work on those things that are not locked (so I don't want to lock the hash itself). The list of which things to lock will change at each call.
It is ok, I think, for the signal loop to be interrupted, as other threads will be cond_wait()'ing on one or more things that they wish to work on.
And yes, I realize that I will need to be extra careful to avoid deadlocks. I am familiar with this from fork/flock. :P
-Colin.
WHITEPAGES.COM | INC
| [reply] [d/l] |
Okay. In order for one thread to wait on the same (sub)set of keys as another thread, there would need to be some way to convey the list of relevant subset between those two threads?
For example, in your code above you have
our @locking_keys = qw/ a b /;
Which presumably would be used by both (all) interested threads.
If there are several different subsets, then there would need to be several keys arrays, or if the keys can vary at runtime, then the keys array would need to be shared between the interested threads.
Either way, using the keys array as your guard variable gives those threads access to the appropriate set of keys and prevents collisions.
I'm not sure if I explained that very well. Basically, for two threads to be able to lock the same set of keys, they both need access to the information telling them what keys are (currently) relevant. That is the perfect variable/array to use as your guard.
BTW. Why are you using our? Any my variable declared prior to a thread being spawned is (implicitly) duplicated into the thread just as an our var is. The difference is that you can :share my vars, but not our vars.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
| [reply] [d/l] [select] |
You have a second problem, You shouldn't assign to $_ without localising it first, because it's often a reference to another variable. Better yet, localize *_.
local *_;
LOCK_LOOP:
$_ = pop @left_to_lock;
lock( $h{ $_ } );
goto LOCK_LOOP if @left_to_lock;
| [reply] [d/l] [select] |
| [reply] |
It's another ugly hack, but I think you could use a string eval. Create the string dynamically, including locks to the variables you need and including the work to be done and the signalling, then eval the string. I'm not sure that's worth it just to avoid a goto call. (You may have just discovered a good use of goto.)
I had to figure out something similar in Object::LocalVars, except I needed to localize a bunch of things instead of locking them. In that case, though, the list of variables is known at compile time, so the eval installs a localizing subroutine only once and efficiency of string eval isn't an issue.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] |