Many threads working constantly


Z

ziemo

Hello,

I need to create 10 threads. When one of them finished work, the next
thread will be start.
I have a problem with memory leak. After couple minute my RAM is full.
Could someone suggest where is the problem or how I can solve my
problem with another method?
Sory for my english and thanks for help :)

ziemo


#!/usr/bin/perl -w
use strict;

use threads;
use threads::shared;
use Thread::Queue;

my $koniec : shared = 1;
my $ilosc_watkow=10;
my $count : shared;
my $end : shared;
my $kon = new Thread::Queue;

while($koniec){
$count = 0;
if($count < 1000){
$end=0;
threads->create(\&watek2) for
1..$ilosc_watkow;
while(!$end){
while($kon->pending){
threads->object($kon->dequeue)-
threads->create(\&watek2) if !
$end;
}
sleep 1;
}
foreach my $thr (threads->list){
if ($thr->tid && !threads::equal($thr,
threads->self)) {
$thr->join;
}
}
}

sleep 5;

}

sub watek2 {
if(!$end){
if($count<1000){
print $count."\n";
lock $count;
$count++
} else {
lock($end);
$end = 1;
}
}
$kon->enqueue(threads->self->tid);

}
 
Ad

Advertisements

X

xhoster

ziemo said:
Hello,

I need to create 10 threads. When one of them finished work, the next
thread will be start.

It is generally better to start your threads up front, and then have them
loop through jobs (like in a Thread::Queue), rather than constantly ending
threads and staring new ones. (Well, I'd argue it generally better, in
Perl, just not to use threads in the first place, but...)
I have a problem with memory leak. After couple minute my RAM is full.
Could someone suggest where is the problem or how I can solve my
problem with another method?

What version are you using? Perl 5.8.0 had a lot of problems with
memory leaks and threads. In 5.8.3, I don't see any leaks in the code you
provided.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top