use strict; use warnings; use threads; use threads::shared; use Thread::Queue; # Constant that hold maximum amount of threads to start use constant MAX_THREADS => 10; # Main data structure that holds all the data my %hash : shared; # A new empty queue my $q = Thread::Queue->new(); # Build list of files my @files = qw/ /; chomp(@files); # Enqueue the files $q->enqueue(map($_, @files)); # Start the threads and wait for them to finish for(my $i=0; $icreate( \&thread, $q )->join; } # Print out the data structure when we're finished foreach my $key1 (keys %hash) { print "$key1 =>\n"; foreach my $key2 (keys %{$hash{$key1}}) { print "\t$key2 =>\n"; print map("\t\t$_\n", @{$hash{$key1}{$key2}}); } } ############################# # This code runs inside of the thread ############################# sub thread { my ($q) = @_; while (my $file = $q->dequeue_nb()) { my @array1 : shared; my @array2 : shared; my @array3 : shared; # Lock the main hash before writing lock(%hash); chomp($file); # Initialize has with the file/key $hash{$file} = &share({}); # Open the file and pattern match the lines open(FH, $file) or die "Can't open\n"; while(my $line = ) { chomp($line); # Build arrays of the things we're # looking for in the file(s) if($line =~ /^/) { push(@array1, $line); } elsif($line =~ /^/) { push(@array2, $line); } elsif($line =~ /^/) { push(@array3, $line); } } close(FH); share ( $hash{$file}{} ); share ( $hash{$file}{} ); share ( $hash{$file}{} ); # Can only assign arrays as a reference $hash{$file}{} = \@array1; $hash{$file}{} = \@array2; $hash{$file}{} = \@array3; } } exit;