Hi,
I have W2k pro sp1 and activestate perl 5.8.0. When I run your code
I also see an increasing memory use, but slow. Without the join()
memory usage rockets, the following code reaches a memory high of 3100 kb
at 50 threads, but over 20000 kb without the join.
#!perl -w
use threads;
my $i;
while(1){
my $thr = threads->new(\&lwp);
$thr->join();
print $i++, "\n";
sleep 2;
};
sub lwp {
return 1;
}
A solution could be to create a fixed number of threads and let them
fetch work from a shared array.
#!perl -w
use strict;
use threads;
use threads::shared;
#put work here
my @work:shared = 1..10;
#create 5 workers
threads->new(\&gotowork) for 1..5;
#wait for all threads to finish
$_->join() for threads->list();
sub gotowork {
while (1) {
my $job;
{
lock @work;
return unless @work > 0;
$job = pop @work;
}
#do something with $job
print "Thread id #" . threads->self->tid() . " doing $job\n";
sleep int rand 4; #simulate lwp request duration
}
}
------------------------------------------------------------------------
! Robert Friberg 0733-839080
! Developer/Trainer perl,java,dotnet,linux,xml,uml,sql,c/c++,vb
! Ensofus AB http://www.ensofus.se/
! Miljo Online AB http://www.miljo-online.se/
------------------------------------------------------------------------