Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

threads::shared hash

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

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


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


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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-01-30 05:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found