Sure, threads could be extended with some convenience functions

#!/usr/bin/perl -- use strict; use warnings; use threads ; use Thread::Queue; Main( @ARGV ); exit( 0 ); sub threads_grep(&@) { my $cb = shift; my $max = int @_; my $args = ref $_[0] ? shift : { slaves => 4, maxdq => $max * 10 +00 }; my $slaves = $args->{slaves} || 4; my $maxdq = $args->{maxdq} || 1e9; my $qin = Thread::Queue->new( @_, ( undef ) x $slaves ); my $qout = Thread::Queue->new(); my @kids = map { threads->create( sub { ## threads_grep_cb my( $cb, $qin, $qout ) = @_; local $_; while( $_ = $qin->dequeue ) { if( $cb->() ) { $qout->enqueue( $_ ); } } warn 'tids ahoy ', threads->tid; return; }, $cb, $qin, $qout ); } 1 .. $slaves; $_->join for @kids; $qin->end; $qout->end; return $qout->dequeue( $maxdq ); } ## end sub threads_grep(&@) sub Main { #~ my @res = threads_grep { $_ % 5 == 0 } 1..1000; my @res = threads_grep { $_ % 5 == 0 } { slaves => 2 }, 1..1000; print "@res\n"; } ## end sub Main __END__ tids ahoy 1 at - line 26. tids ahoy 2 at - line 26. 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 11 +5 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 26 +5 270 275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360 365 370 375 380 385 390 395 400 405 410 41 +5 420 425 430 435 440 445 450 455 460 465 470 475 480 485 490 495 500 505 510 515 520 525 530 535 540 545 550 555 560 56 +5 570 575 580 585 590 595 600 605 610 615 620 625 630 635 640 645 650 655 660 665 670 675 680 685 690 695 700 705 710 71 +5 720 725 730 735 740 745 750 755 760 765 770 775 780 785 790 795 800 805 810 815 820 825 830 835 840 845 850 855 860 86 +5 870 875 880 885 890 895 900 905 910 915 920 925 930 935 940 945 950 955 960 965 970 975 980 985 990 995 1000

In reply to Re^3: Wanting some clarification / opinions on MCE vs Threads ( threads_grep ) by Anonymous Monk
in thread Wanting some clarification / opinions on MCE vs Threads by jmmitc06

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.