in reply to Looping without introducing a block
My first reaction was what's wrong with:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Looping without introducing a block
by cmeyer (Pilgrim) on Aug 03, 2005 at 17:02 UTC | |
by BrowserUk (Patriarch) on Aug 03, 2005 at 17:27 UTC |