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.
|
---|
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 | |
by BrowserUk (Patriarch) on May 13, 2015 at 21:23 UTC | |
Re^2: Threads From Hell #1: How To Share A Hash
by karlgoethebier (Abbot) on May 13, 2015 at 20:11 UTC |