Hello all,

I have a pool of file and I want to process this files by threads opened
on start.

The first free thread get the first file on pool, remove from list and
process this file, next thread takes the next file, ...

If the pool is empty the threads stay waiting for new files.

I'm trying to do something like this script below, but the threads can't
see the queue list.

Thanks for help!

Regards

#!/usr/bin/perl 

use Thread qw(yield);
use Data::Dumper;
use Thread::Queue;
use threads::shared;

my $DataQueue : shared = Thread::Queue->new;

# start threads
my $thr1 = new Thread \&threadWorker;
my $thr2 = new Thread \&threadWorker;
my $thr3 = new Thread \&threadWorker;
my $thr4 = new Thread \&threadWorker;

while (1) {
        $DataQueue->enqueue( time() );
        print " Added: " . time() . " / Total: " . $DataQueue->pending .
"\n";
        sleep(1);
}


sub threadWorker {
        my ($DataElement);

        while (1) {

                if ( $DataElement = $DataQueue->dequeue_nb ) {
                        print "- $DataElement popped\n";
                } else {
                        print "DataQueue is empty [" . $DataQueue->pending
 "] ";
                print "- " . Dumper($DataQueue);
                }
                sleep 1;
        }

        return 1;
}



Reply via email to