I have a question about ithreads. I would like to lock an arbitrary list of shared items, do a bunch of work on them (together), and then signal some other threads. So, I would like to loop over the list of shared items, and lock them. The trouble is that thread locks are block scoped in Perl, and normal loops introduce another block context. At the end of the loop, the block exits, and I loose all of my locks.
The work around that I have is to use goto!
Does anyone with ithread experience have a better approach to this problem? If you are at OSCON, and have real experience with Perl's ithreads, then I would love to talk to you! Please /msg me.
Here's some test code:
Edited: minor code fix ala ikegami's suggestion, and another comment to make it clear that I need all of the items locked simultaneously.#!/usr/local/perl_ithreads/bin/perl use strict; use warnings; use threads; use threads::shared; our %h = ( a => 1, b => 2, c => 3 ); share( $h{ a } ); share( $h{ b } ); share( $h{ c } ); our @locking_keys = qw/ a b /; { # this loop introduces another block context lock( $h{ $_ } ) for @locking_keys; # here I wish to munge all of the lock # things at the same time, say copy data # from one to another. # this gives a warning, because the block # context of the above loop has already been # exited, causing the locks to be lost cond_signal( $h{ $_ } ) for @locking_keys; } { my @left_to_lock = @locking_keys; my $key; LOCK_LOOP: $key = pop @left_to_lock; lock( $h{ $key } ); goto LOCK_LOOP if @left_to_lock; # now the things are really locked, and so no warning is issued here cond_signal( $h{ $_ } ) for @locking_keys; }
-Colin.
WHITEPAGES.COM | INC
In reply to Looping without introducing a block by cmeyer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |