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

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.

Replies are listed 'Best First'.
Re^3: Locking hash values
by ikegami (Patriarch) on Dec 18, 2006 at 18:36 UTC

    I think that should be

    lock ${$hash{key}};

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

      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.
      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.