Update: after reading Re: What is the fastest way to download a bunch of web pages? i've reworked this function to follow the same pattern. Notice that Thread::Queue only takes scalars, so if you want to push funky data through the queue, one way is to freeze/thaw it.
use strict; use warnings; use Data::Dumper; use threads; use Thread::Queue; use Storable qw(freeze thaw); my $slow_matches_b = sub { sleep 1; return unless $_[0]; return 1 if $_[0] =~ /b/; }; my $test_strings = [ ('blee','blah','bloo', 'qoo', 'fwee' ) ]; my @results = threaded_map( 2, $slow_matches_b, @$test_strings ); print Dumper \@results; sub threaded_map { my $thread_count = shift; my $function = shift; # # setup work queues # my $id = 0; my $input_q = new Thread::Queue; my $result_q = new Thread::Queue; for (map { [ $id++, $_ ] } @_) { $input_q->enqueue(freeze($_)); } # # define worker function to run in each thread # my $worker_function = sub { while ( my $frozen_work = $input_q->dequeue_nb ) { my $work = thaw($frozen_work); my $id = $work->[0]; my $input_data = $work->[1]; my $result = $function->($input_data); my $frozen_result = freeze( [ $id, $result ] ); $result_q->enqueue($frozen_result); } }; # # create workers that will read from shared input queue and wri +te to shared output queue # my @threads = map{ threads->create( $worker_function ) } 1 .. $thr +ead_count; $_->join for @threads; # # join the results data together # my %results; while ( my $frozen_result = $result_q->dequeue_nb ) { my $result = thaw($frozen_result); $results{$result->[0]} = $result->[1]; } # # return results in order received # return map { $results{$_} } sort keys %results; }

In reply to threaded map by LanceDeeply
in thread Using functional programming to reduce the pain of parallel-execution programming (with threads, forks, or name your poison) by tphyahoo

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.