in reply to Threads From Hell #1: How To Share A Hash [SOLVED]

You're trying to assign a Math::BigInt object to a shared hash -- hence the "Invalid value for shared scalar" msg.

You can however assign the value of that object:

use strict; use threads; use threads::shared; use Math::BigInt; use Data::Dump qw[ pp ]; use feature qw(say); my @numbers = ( 1 .. 10 ); my %result : shared; my @threads = map { threads->create( \&process, $_ ); } @numbers; $_->join for @threads; for my $key ( sort{ $a <=> $b } keys %result ) { say "$key : $result{ $key }"; } sub process { my $number = shift; my $factorial = factorial($number); lock %result; $result{$number} = $factorial->bstr; ### Extract the value from th +e object } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

Produces:

C:\test>junk 1 : 1 2 : 2 3 : 6 4 : 24 5 : 120 6 : 720 7 : 5040 8 : 40320 9 : 362880 10 : 3628800
Corion kindly advised my not to use Thread::Semaphore ... But i couldn't resist to use Thread::Semaphore

Dunno quite what you thought you were achieving with it, but whatever it was, it wasn't doing anything useful.


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". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: Threads From Hell #1: How To Share A Hash
by karlgoethebier (Abbot) on May 13, 2015 at 21:16 UTC
    "...whatever it was, it wasn't doing anything useful."

    Well, i would say it depends.

    It was for learning purposes, a kind of finger exercise.

    I didn't mention that i don't have any experience using threads in Perl. My first approach.

    I always felt like i'll miss something if i don't go over that next bridge.

    My little effort was also inspired by my last thread about P::FM, because that stuff doesn't work on windows. I am sorry that i can't show the error messages, because i don't have a windows box with Perl installed at my disposal tonight.

    Also pure curiosity played a role. I have a basic understanding how a counting semaphore works but this stuff is still a kind of magic for me.

    And the code i figured out looks very similar to my forking example and doesn't perform so bad.

    That's why i couldn't resist.

    My very best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      And the code i figured out looks very similar to my forking example and doesn't perform so bad.

      So... you were trying to restrict the number of concurrent threads to 4? Yes. It works for that purpose.

      (I originally thought is was some attempt to prevent concurrent access to the shared hash; ie. a substitute for lock. My mistake.)


      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". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re^2: Threads From Hell #1: How To Share A Hash
by karlgoethebier (Abbot) on May 13, 2015 at 20:11 UTC
    "...assign a Math::BigInt object...assign the value of that object...

    Do'h! That hurts.

    In German it (my behavior) is called "Den Wald vor lauter Bäumen nicht sehen" which should translate to "to miss the forest for the trees" - i think.

    Update: Minor change in wording to avoid misunderstanding.

    Thank you very much for advice and my best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»