Hi
I have the following code that creates workers processing a queue. The problem I have is when to undefine the queue. My only solution I found was to create an array to hold all threads that are idling. When the array count is equal the thread count I undefine the queue and join the threads.
I am sure there is better / cleaner ways to do that..
Any help would be appreciated.
sub pullDataFromDbWithDirectory {
my $_dir = $_[0];
if ($itemCount <= $maxNumberOfItems) {
my @retval = grep { /^Dir|^File/ } qx($omnidb -filesystem $fil
+esystem '$label' -listdir '$_dir');
foreach my $item (@retval) {
$itemCount++;
(my $filename = $item) =~ s/^File\s+|^Dir\s+|\n//g;
my $file = "$_dir/$filename";
push(@data,$file);
if ($item =~ /^Dir/) {
$worker->enqueue($file);
print "Add $file to queue\n" if $debug;
}
}
}
}
sub doOperation () {
my $ithread = threads->tid();
do {
my $folder = $worker->dequeue();
print "Read $folder from queue with thread $ithread\n" if $debu
+g;
pullDataFromDbWithDirectory($folder);
} while ($worker->pending());
push(@IDLE_THREADS,$ithread);
}
This is the main section
my @threads = map threads->create(\&doOperation), 1 .. $maxNumberOfPar
+allelJobs;
pullDataFromDbWithDirectory($directory);
sleep 0.01 while (scalar @IDLE_THREADS < $maxNumberOfParallelJobs);
$worker->enqueue((undef) x $maxNumberOfParallelJobs);
$_->join for @threads;
printData();