in reply to wait for threads to end in parallel
There's no need to create a new thread for each file.
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();
Or since you never add anything to @file_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();
( Oops, way too slow at posting this )
|
|---|