Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

threads::shared hash

by fxmakers (Friar)
on Mar 27, 2004 at 00:24 UTC ( [id://340172]=perlquestion: print w/replies, xml ) Need Help??

fxmakers has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

Here's my code:
use threads; use threads::shared; my %hash : shared; my $var = "hello"; $hash{'foo'}{'bar'} = 1; $hash{$var}{'bar'} = 1;
Script is not interpreted and returns: Invalid value for shared scalar at test.pl line 7.
Can't create a shared hash of hashes.
$hash{'foo'} = 1; #works fine.
perl -v: This is perl, v5.8.2 built for cygwin-thread-multi-64int

Any ideas?
Thanks!

-UPDATED POST-

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: threads::shared hash
by BrowserUk (Patriarch) on Mar 27, 2004 at 01:01 UTC

    Seems to be a problem with your build of perl rather than perl itself, in that I don't get any errors (other than the missing semicolons) running your snippet under 5.8.2/AS 808

    This is perl, v5.8.2 built for MSWin32-x86-multi-thread Binary build 808 provided by ActiveState Corp. http://www.ActiveState. +com

    That said, your snippet obviously isn't a direct c&p of your failing code (given the syntax errors), and the partial error message you give "Invalid value for shared scalar at..." is usually a compile or runtime error, not a crash. So maybe you should post a more complete example?

    P:\test>perl -Mthreads -mthreads::shared my( %h ) : shared; my $var = 'hello'; $h{ $var } = 123; print $h{hello}; ^Z 123

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      You're right, I did some mistakes, I just modified my post :P
      A shared hash works fine, but
      the creation of a shared hash of hashes is the problem:

      use threads;
      use threads::shared;
      use strict;

      my %hash : shared;
      my $var = "hello";

      $hash{'foo'}{'bleh'} = "test";
      $hash{$var}{'foo'} = "test";

      Both 'foo' or $var keys returns the error: Invalid value for shared scalar.

        Unfortunately, autovivification doesn't auto-share, so you have to manually create shared elements.

        #! perl -slw use strict; use threads; use threads::shared; use Data::Dumper; my %hash : shared; my $var = 'hello'; $hash{ foo } = &share( {} ); $hash{ foo }{ bleh } = 'test'; $hash{ $var } = &share( {} ); $hash{ $var }{ foo } = 'test'; print Dumper \%hash; __END__ P:\test>test $VAR1 = { 'foo' => { 'bleh' => 'test' }, 'hello' => { 'foo' => 'test' } };

        Note: The &shared( {} ); syntax is required. See threads::shared for details.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
Re: threads::shared hash
by tilly (Archbishop) on Mar 27, 2004 at 07:20 UTC
    Random advice.

    Unless you can give a really good reason why you should, you probably don't want to use Perl's thread implementation. If you think that you have a good reason, I'd still not recommend pushing it very far.

Log In?
Username:
Password:

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

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

    No recent polls found