You could do it the easy way.

#! perl -slw use strict; use threads qw[ async yield ]; use Thread::Queue; my $Qrv = new Thread::Queue; my @work = qw[ C: D: P: Q: T: U: V: W: Z: ]; for ( 1 .. 2 ) { my $cmd = join ' ', 'dir /s', shift( @work ), '>nul'; async( sub{ $Qrv->enqueue( "$_: " . system( $cmd ) ); } )->detach; } while( @work ) { my( $threadNo, $rv ) = split ': ', $Qrv->dequeue(); my $cmd = join ' ', 'dir /s', shift( @work ), '>nul'; async( sub{ $Qrv->enqueue( "$threadNo: " . system( $cmd ) ); } )-> +detach; } sleep 2;

A slightly more verbose version shows the workings

#! perl -slw use strict; use threads qw[ async yield ]; use Thread::Queue; my $Qrv = new Thread::Queue; my @work : shared = qw[ C: D: P: Q: T: U: V: W: Z: ]; for (1..2) { my $work = shift( @work ); print "Starting '$work'"; my $cmd = "dir /s $work >nul"; my $thread = async{ $Qrv->enqueue( "$_: " . system( $cmd ) ); }; $thread->detach(); } while( @work ) { print "Work left: @work"; my( $threadNo, $rv ) = split ': ', $Qrv->dequeue(); printf "Thread: $threadNo returned %d\n", $rv>>8; my $work = shift( @work ); print "Starting '$work'"; my $cmd = "dir /s $work >nul"; my $thread = async{ $Qrv->enqueue( "$threadNo: " . system( $cmd ) +); }; $thread->detach(); } # Ought to yield here until no threads are left running # but the API doesn't provide any way to find out how many detached th +reads # are left running. # Equally, there is no way to join() the next thread to finish. sleep 2; __END__ P:\test>357420 Starting 'C:' Starting 'D:' Work left: P: Q: T: U: V: W: Z: Thread: 1 returned 0 Starting 'P:' Work left: Q: T: U: V: W: Z: Thread: 1 returned 0 Starting 'Q:' Work left: T: U: V: W: Z: Thread: 1 returned 0 Starting 'T:' Work left: U: V: W: Z: Thread: 1 returned 0 Starting 'U:' Work left: V: W: Z: Thread: 1 returned 0 Starting 'V:' Work left: W: Z: Thread: 1 returned 0 Starting 'W:' Work left: Z: Thread: 2 returned 0 Starting 'Z:'

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Keep Two Processes Going by BrowserUk
in thread Keep Two Processes Going by blax

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.