#! 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; #### #! 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 threads # 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:'