in reply to Locking hash values

You can sorta get this to work like so:
my $scalar : shared = 0; my %hash; $hash{key} = \$scalar; lock $hash{key};

I'm not sure how reliable that is when you start new threads though. You're probably better off just sticking with a few shared scalars in my opinion. Perl's threading isn't one of it's strongest features. At least you're not stuck with IPC.

-Paul

Replies are listed 'Best First'.
Re^2: Locking hash values
by bucky0 (Initiate) on Dec 18, 2006 at 18:25 UTC
    Perhaps I'm doing something wrong, but that code fails out as well...perl's threading hurts me, this should be a simple thing to do. Not only that, it's mentioned in the POD as working.

      I think that should be

      lock ${$hash{key}};

      The idea is that $scalar is shared, and therefore lockable.

        To continue with this theme why not to share a ref to a hash. Like so:
        ... use threads; use threads::shared; ... my $href : shared; $href = &share({}); ... # Lock block { lock $href; $href->{mykey} = 'myval'; } ...
        For more see perldoc threads::shared.

        - BR

        That works out well. It's weird that it's mentioned in the POD as something that should work without all of this hoopla. The design behind perl threads _really_ wants to make it hard to share data, it seems.