Hi all,

i figured this out by RTFM:

#!/usr/bin/env perl use strict; use warnings; use threads; use threads::shared; use Thread::Semaphore; use Math::BigInt; use Data::Dump; use feature qw(say); my @numbers = ( 1 .. 10 ); my %result : shared; my $semaphore = Thread::Semaphore->new(4); my @threads = map { $semaphore->down; threads->create( \&process, $_ ); } @numbers; $_->join for @threads; dd \%result; sub process { my $number = shift; my $factorial = factorial($number); say qq($number $factorial); eval { $result{$number} = $factorial; }; say $@ if $@; $semaphore->up; } sub factorial { my $number = shift; Math::BigInt->bfac($number); } __END__ karls-mac-mini:monks karl $ ./semaphore.pl ... 10 3628800 Invalid value for shared scalar at ./semaphore.pl line 29. ...

It works, but the only thing i couldn't manage is to assign the results to a hash.

I did some more RTFM about how to accomplish this but without success.

What do i miss?

Thank you very much for any hint and best regards,

Karl

P.S.: I asked something similar at noon it the CB and posted an example in my scratchpad. Corion kindly advised my not to use Thread::Semaphore and pointed me to the canonical advice given by BrowserUK which uses Thread::Queue. But i couldn't resist to use Thread::Semaphore

«The Crux of the Biscuit is the Apostrophe»


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.