in reply to Re^2: Locking hash values
in thread Locking hash values

I think that should be

lock ${$hash{key}};

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

Replies are listed 'Best First'.
Re^4: Locking hash values
by bucky0 (Initiate) on Dec 18, 2006 at 18:55 UTC
    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.
Re^4: Locking hash values
by caelifer (Scribe) on Dec 18, 2006 at 19:35 UTC
    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

      If the OP wanted to lock the hash, lock(%hash) would be sufficient. However, he wants to lock an element of the hash.

        yeah, I could lock the whole hash, but I'd much rather be able to lock hash-values to keep from blocking too much.