use strict; use warnings; use threads; use Thread::Queue; use constant MAXTHREADS => 1; # Update this to the maximum number of threads you want. Keep it below the number of physical cores available on your machine. my @inputFiles = qw(); # Do something to populate your list of inputs my $inputQueue = Thread::Queue->new( @inputFiles ); my $printQueue = Thread::Queue->new(); $inputQueue->end(); # Tell everyone that the inputQueue wont have anything else added to it my $printThread = threads->new( \&printWorker ); my @workerThreads = map { threads->new( \&worker ) } 1..MAXTHREADS; $_->join for @workerThreads; $printQueue->end(); # Tell everyone that the printQueue wont have anything else added to it $printThread->join(); ###### END OF MAIN ###### sub worker { # Add code to prepare for individual file processing # This function should be the equivalent of your (working) serial process while ( defined( my $file = $inputQueue->dequeue() ) ) { # Add code here to process individual files $printQueue->enqueue( 'output' ); # Instead of printing, enqueue output data to the print queue } } sub printWorker { my $outFile = "myOutFile"; # Set name of output file open my $fhOut, ">", $outFile or &{ warn "Failed to open file for writing: $!"; exit 1; }; # Kill all threads on failure select $fhOut; print $_ while $_ = $printQueue->dequeue(); close $fhOut; }