For Programmers: Free Programming Magazines  


Home > Archive > ithreads > June 2005 > Newbie user! Anybody can help me?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie user! Anybody can help me?
PerlDiscuss - Perl Newsgroups and mailing lists

2005-06-08, 9:03 pm

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;
}



Mike Pomraning

2005-06-08, 9:03 pm

On Fri, 3 Sep 2004, PerlDiscuss - Perl Newsgroups and mailing lists wrote:

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


Why not use dequeue(), which is blocking?

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


Do you mean that the worker threads see $DataQueue as perpetually empty, or
what?

At any rate, your example seems to work for me with the following significant
changes, under 5.8.0 (old!):

- 'threads' instead of 'Thread'
threads->create(\&threadWorker); # instead of new Thread ...

- omit threads::shared
my $DataQueue = Thread::Queue->new; # automatically shared

Modified code:

--------------------
#!/usr/bin/perl

use threads;
use Thread::Queue;

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

# start threads
threads->create(\&threadWorker, $_) for 1 .. 4;

while (1) {
$DataQueue->enqueue( my $item = time() );
print "[producer] Added: $item / Total: ", $DataQueue->pending, "\n";
sleep 1;
}

sub threadWorker {
my $WorkerID = shift;
sleep 5;
while (1) {
if ( my $DataElement = $DataQueue->dequeue_nb ) {
print "[consumer $WorkerID] popped $DataElement\n";
} else {
print "[consumer $WorkerID] queue empty [", $DataQueue->pending, "]\n";
}
sleep 1;
}
return 1;
}
--------------------

Regards,
Mike
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com