I had always wanted to share a solution using MCE which comes with a sequence generator. This allows one to use a pool of workers seamlessly while iterating through a sequence of numbers.

The first example is a modified version of the demonstration by BrowserUk.

use strict; use threads; use threads::shared; use MCE::Flow; use Math::BigInt; use feature qw(say); my %result : shared; mce_flow_s { chunk_size => 1, max_workers => 4 }, \&process, 1, 1000; for my $key ( sort{ $a <=> $b } keys %result ) { say "$key => ", $result{ $key }->bstr; } sub process { my $number = $_; my $factorial = factorial($number); $result{$number} = shared_clone( $factorial ); ## clone the object } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

The next example requires the upcoming MCE 1.7 release, currently residing in GitHub. Notice the use of MCE::Shared and the capital letter S in Shared for the variable attribute.

use strict; use MCE::Flow; use MCE::Shared; use Math::BigInt; use feature qw(say); my %result : Shared; mce_flow_s { chunk_size => 1, max_workers => 4 }, \&process, 1, 1000; for my $key ( sort{ $a <=> $b } keys %result ) { say "$key => ", $result{ $key }->bstr; } sub process { my $number = $_; my $factorial = factorial($number); $result{$number} = $factorial; # the object is shared automatical +ly } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

Both complete in the same time frame. Threads is not required to run MCE::Shared as it supports threads and processes alike. Thus, one may include threads if desired.

Init is another way to specify MCE options.

MCE::Flow::init { chunk_size => 1, max_workers => 4, }; mce_flow_s \&process, 1, 1000;

Kind regards, Mario


In reply to Re: Threads From Hell #1: How To Share A Hash [SOLVED] by marioroy
in thread 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.