in reply to how to use threads for this problem?
A simple demonstration of processing subdirs in parallel. This just accumulates the number of bytes used by the files in the list of (relative) directories supplied on the command line, but it may give you a start in what you are trying to do.
#! perl -slw use strict; use threads; sub worker { my( $dir ) = @_; my $total = 0; opendir my $dh, $dir or die "$dir : $!"; while( my $file = readdir $dh ) { $total += -s "$dir/$file"; } closedir $dh; return $total; } my @threads; for my $dir ( @ARGV ) { if( -d $dir ) { push @threads, threads->new( \&worker, $dir ); } } my $total; $total += $_->join for @threads; print "Total bytes in files in directories [@ARGV] is: ", $total; __END__ C:\test>612156 3 4 5 data Total bytes in files in directories [3 4 5 data] is: 654203874
You don't say what information you are retrieving from your processing of the directories that you need for subsequent processing, but if it is much more than one or two values, it might be better to return them to the main thread via a queue or other shared datastructure rather than via return/join, but one item, or a small list of items, is okay done this way.
|
|---|