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:

#!/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; }
Edited: minor code fix ala ikegami's suggestion, and another comment to make it clear that I need all of the items locked simultaneously.

-Colin.

WHITEPAGES.COM | INC


In reply to Looping without introducing a block by cmeyer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.