Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by ISAI student (Scribe)
on Jan 02, 2013 at 12:31 UTC ( [id://1011258]=note: print w/replies, xml ) Need Help??


in reply to Re: Shared hash within shared hash of hash???
in thread Shared hash within shared hash of hash???

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?

Replies are listed 'Best First'.
Re^3: Shared hash within shared hash of hash???
by dave_the_m (Monsignor) on Jan 02, 2013 at 17:30 UTC
    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.

Re^3: Shared hash within shared hash of hash???
by BrowserUk (Patriarch) on Jan 02, 2013 at 15:53 UTC
    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.
Re^3: Shared hash within shared hash of hash???
by zwon (Abbot) on Jan 02, 2013 at 12:46 UTC
    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.
        I did, it seems to work

        That's strange, because for me it didn't work:

        use threads; use threads::shared; my %sa : shared = (); %{$sa{foo}} = (); __END__ Invalid value for shared scalar at share_share.pl line 5.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-03-19 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found