UpTide, welcome to the monastery.

The following is a modification by BrowserUk's demonstration. Task A, B, C are spawned with 1, 2, 3 workers respectively. If threads is desired, load threads before MCE. By default, MCE spawns child processes on UNIX and Cygwin.

#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; sub task_a { my ($Qb, $Qc) = @_; for (1 .. 1e5) { $Qb->enqueue(1e3 + int(rand 1000)); $Qc->enqueue(2e3 + int(rand 1000)); } $Qb->enqueue(undef) for 1 .. 2; } sub task_b { my ($Qb, $Qc) = @_; while ( my $arg1 = $Qb->dequeue() ) { $Qc->enqueue($arg1 * 10); } $Qc->enqueue(undef) for 1 .. 3; } sub task_c { my ($Qc) = @_; while ( my $arg1 = $Qc->dequeue() ) { print "$arg1\n"; } } my $Qb = new Thread::Queue; my $Qc = new Thread::Queue; my @thrs; push @thrs, threads->new( \&task_a, $Qb, $Qc ) for 1 .. 1; push @thrs, threads->new( \&task_b, $Qb, $Qc ) for 1 .. 2; push @thrs, threads->new( \&task_c, $Qc ) for 1 .. 3; $_->join for @thrs; print "Done\n";

The following does the same thing using the MCE module. The await method prevents the queue from exceeding 200 items.

#!/usr/bin/perl use strict; use warnings; use MCE::Step; sub task_a { for (1 .. 1e5) { MCE->await('B', 200) if $_ % 200 == 0; MCE->enq('B', 1e3 + int(rand 1000)); MCE->await('C', 200) if $_ % 200 == 0; MCE->enq('C', 2e3 + int(rand 1000)); } } sub task_b { my ($mce, $arg1, $arg2, $argN) = @_; MCE->enq('C', $arg1 * 10); } sub task_c { my ($mce, $arg1, $arg2, $argN) = @_; print "$arg1\n"; } MCE::Step->init( task_name => [ 'A', 'B', 'C' ], max_workers => [ 1, 2, 3 ], ); mce_step \&task_a, \&task_b, \&task_c; print "Done\n";

In reply to Re: Data Between Threads by Anonymous Monk
in thread Data Between Threads by UpTide

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.