Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Shared hash within shared hash of hash???

by zwon (Monsignor)
on Jan 02, 2013 at 11:43 UTC ( #1011250=note: print w/ replies, xml ) Need Help??


in reply to Shared hash within shared hash of hash???

threads::shared keeps all shared variables in a separate interpreter. If you declared hash like:

my %hash : shared = ();
then hash and all its values will be stored in this dedicated interpreter. In case of:
$hash{foo} = {};
you creating anonymous hash in the current interpreter and saving reference to it in the shared interpreter which is not allowed, that's why you should create a shared copy:
$hash{foo} = shared_clone({});
Another possibility:
my %tmp : shared = (); $hash{foo} = \%tmp;


Comment on Re: Shared hash within shared hash of hash???
Select or Download Code
Re^2: Shared hash within shared hash of hash???
by ISAI student (Scribe) on Jan 02, 2013 at 12:31 UTC
    I seem to be getting better understanding...

    Two questions:

    1. Do i need to use  lock $hash{foo} ;
    or lock ( \%hash ) ; if foo is used as shared_clone ?

    2. Can I just use
    %{$hash{foo}} = () ;
    And just lock %hash, if needed?
      I'm not sure about the first one, as for the second -- why don't you try it?
        I did, it seems to work. I am however, wary of corner cases, when threads are involved.
      1. Do i need to use lock $hash{foo} ; or lock ( \%hash ) ; if foo is used as shared_clone ?

      It depends upon what you intend to modify.

      When you have nested hashes so:

      %hash :shared = &share( {} );
      1. To add a new key/value pair; or delete a key/value pair; or modify the value associated with an existing key in %hash you need:
        lock %hash; $hash{ existingkey } = newvalue; # or $hash{ newkey } = newvalue; #or delete $hash{ existingkey };
      2. To modify the nested anonymous hash associated with the key 'foo', you need:
        lock %{ $hash{ foo } }; $hash{foo}{somekey} = somevalue; # or delete $hash{ foo }{ somekey };

        Ie. lock %{ $h{1} } says lock the hash referenced by the value at $h{1}.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Do i need to use lock $hash{foo} ; or lock ( \%hash ) ; if foo is used as shared_clone ?
      You only need locking to ensure the consistency of your own data when shared between threads. Perl already does its own internal locking on shared structures to ensure internal consistency. So for example
      my %h : shared; $h{foo} = share ({}); ... $h{foo}{bar} = 1; # in thread 1 $h{foo}{bar} = 2; # in thread 2
      Here, there is no locking, so $h{foo}{bar} may end up as 1 or 2; but it won't end up as something else; nor will the {foo} or the {bar} slots of the respective hashes get corrupted.

      Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011250]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-08-01 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls