use threads; use threads::shared; use Thread::Queue; my @file_list = @ARGV; my $num_workers = 2; my %MSG :shared; sub process { my ($file) = @_; print "Opening $file\n"; if(some condition){ lock(%MSG); #write stuff to hash } print "Closing $file\n"; } my $q = Thread::Queue->new( @file_list, (undef)x$num_workers, ); for (1..$num_workers) { async { while (defined(my $file = $q->dequeue())) { process($file); } } } $_->join() for threads::list(); #### use threads; use threads::shared; use Thread::Queue; my @file_list = @ARGV; my $num_workers = 2; my %MSG :shared; sub process { my ($file) = @_; print "Opening $file\n"; if(some condition){ lock(%MSG); #write stuff to hash } print "Closing $file\n"; } my $q = Thread::Queue->new(@file_list); for (1..$num_workers) { async { while (defined(my $file = $q->dequeue_nb())) { process($file); } } } $_->join() for threads::list();