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

Alternatively, if you want/need to keep the Math::BigInt object intact, you can use threads::shared::shared_clone:

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 }, ' => ', $result{ $key }->bstr; } sub process { my $number = shift; my $factorial = factorial($number); lock %result; $result{$number} = shared_clone( $factorial ); ## clone the object } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

Produces:

C:\test>junk 1 : Math::BigInt=HASH(0x3f5d160) => 1 2 : Math::BigInt=HASH(0x3f5d190) => 2 3 : Math::BigInt=HASH(0x3f5d148) => 6 4 : Math::BigInt=HASH(0x3f5d178) => 24 5 : Math::BigInt=HASH(0x3f5d118) => 120 6 : Math::BigInt=HASH(0x3f5d130) => 720 7 : Math::BigInt=HASH(0x3f5d160) => 5040 8 : Math::BigInt=HASH(0x3f5d190) => 40320 9 : Math::BigInt=HASH(0x3f5d148) => 362880 10 : Math::BigInt=HASH(0x3f5d178) => 3628800

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 14, 2015 at 17:13 UTC

    Thanks again BrowserUK.

    But must i really say lock %result?

    Without it the calculation for 2000 numbers is about two times faster twice as fast.

    Edit: Minor change of wording.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      But must i really say lock %result?

      If you can guarantee no duplicates in your list of numbers; then no.


      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
        "...guarantee no duplicates..."

        Sure can i - in this case ;-)

        But the code works with dups, whether i use lock or not.

        I tried this:

        sub process { my $number = shift; # lock %result; $result{ threads->tid() } = shared_clone( { $number => factorial($number) } ); $semaphore->up; }

        It works as expected:

        ./semaphore.pl 0.0276410579681396 { # tied threads::shared::tie 1 => { # tied threads::shared::tie 10 => 3628800, }, 2 => { # tied threads::shared::tie 10 => 3628800, }, 3 => { # tied threads::shared::tie 10 => 3628800, }, 4 => { # tied threads::shared::tie 11 => 39916800, }, }

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»