Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Threading: Invalid value for shared scalar

by P0w3rK!d (Pilgrim)
on May 20, 2003 at 20:18 UTC ( #259568=note: print w/replies, xml ) Need Help??


in reply to Re: Threading: Invalid value for shared scalar
in thread Threading: Invalid value for shared scalar

I put in a request to fix the code. It was not filled at the time of your review.

I'm going to have to create a separate shared hash of keys to mainpulate separate unshared hash (n) times. What a pain!!! :(

-P0w3rk!d

  • Comment on Re: Re: Threading: Invalid value for shared scalar

Replies are listed 'Best First'.
Re^3: Threading: Invalid value for shared scalar
by spazm (Monk) on Oct 01, 2009 at 21:06 UTC
    You can share nested hash, as long as the internal hashes are also marked as shared. Let's say your non-shared code to populate the hash looked like this:
    #non shared hash version my %abc; my @parents = qw( a b c ); my @children = qw( 1 2 3 4 ); for my $parent ( @parents ) { for my $child ( @children ) { $abc{ $parent }{ $child } = 1; } }
    Changing the implicit hash creation to explicit hash creation will fix the problem. the perl thread model is predicated on non-shared data by default. This forces us to deliberately share all the structures. (It is a pain).
    # shared hash version. my %abc : shared ; my @parents = qw( a b c ); my @children = qw( 1 2 3 4 ); for my $parent ( @parents ) { unless (exists $abc{$parent}) { my %p : shared; $abc{ $parent } = \%p; } for my $child ( @children ) { unless ( exists $abc{$parent}{$child} ) { my %c : shared; $abc{$parent}{$child}=\%c; } $abc{ $parent }{ $child } = 1; } }
    The lexical scopes for %p and %c will go away, but the references will remain in the hash, preventing garbage collection.
      Very good explanation, thanks!

      But in the second example this

      ... unless ( exists $abc{$parent}{$child} ) { my %c : shared; $abc{$parent}{$child}=\%c; } ...
      looks like an superficial assignment of $abc{ $parent }{ $child } since immediately thereafter the reference \%c is overwritten by a scalar. For two levels of hashing two shared hashes should be enough (%abc and %p).

      So for this example the following minimal version should work as well:

      # shared hash version. my %abc : shared ; my @parents = qw( a b c ); my @children = qw( 1 2 3 4 ); for my $parent ( @parents ) { unless (exists $abc{$parent}) { my %p : shared; $abc{ $parent } = \%p; } for my $child ( @children ) { $abc{ $parent }{ $child } = 1; } }
      For all Perl people who got a bit confused by the example code like I was...

        Or just:

        my @parents = qw( a b c ); my @children = qw( 1 2 3 4 ); my %abc :shared; %{ $abc{ $_ } //= &share({}) } = map{ $_ => 1 } @children for @parents +; pp \%abc;

        Which prints:

        do { my $a = { # tied threads::shared::tie a => { # tied threads::shared::tie 1 => 1, 2 => 1, 3 => 'fix', 4 => 'fix', }, b => { # tied threads::shared::tie 1 => 1, 2 => 1, 3 => 'fix', 4 => 'fix', }, c => { # tied threads::shared::tie 1 => 1, 2 => 1, 3 => 'fix', 4 => 'fix', }, }; $a->{a}{3} = $a->{a}{1}; $a->{a}{4} = $a->{a}{2}; $a->{b}{3} = $a->{b}{1}; $a->{b}{4} = $a->{b}{2}; $a->{c}{3} = $a->{c}{1}; $a->{c}{4} = $a->{c}{2}; $a; }

        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.
        I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      This method tested work, thanks a lot.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2019-12-06 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (154 votes). Check out past polls.

    Notices?