Home: Perl Programming Help: Advanced:
threads queue and memory leak

New User

Dec 4, 2013, 10:06 PM

Views: 13133
threads queue and memory leak

Hi all ,

I am using threads in my code , and somewhere in Perl documentation I saw that I shall use queues for threading - if I dont want any memory leaks.
Today , I have big scripts that can simoultaniously run 24 threads , which I can see - after the threads end, the memory doesnt clean itself.
I read about queues to threads in Perl , but I didnt got the idea, so I need a little help there.
Is the idea is creating some pool of lets say 10 threads , and every time there is a job - the job is sent to an available thread ?
my need is to each thread to return something when it ends - which I put inside a hash , and then use this hash.
Can some one help me with that and explain me the theory ?
thanks !

this is my current code

   #Start new thread 

my $t1 = threads->new (\&$functions_name1,@parameters1);

my $t2 = threads->new (\&$functions_name2,@parameters2);

my $index=0;
my %hash_results = ();
foreach my $thread (@threads) #wait for all threads untill the end and insert results to hash

Veteran / Moderator

Dec 5, 2013, 10:49 AM

Views: 13088
Re: [mojo2405] threads queue and memory leak

This is cross posted on the Perl Monks firum, where a number of answers have already been provided.

New User

Dec 6, 2013, 4:19 AM

Views: 13037
Re: [mojo2405] threads queue and memory leak

problem solved using this code.

use 5.010; 
use strict;
use warnings;

use Parallel::ForkManager;
use Data::Printer;

my $pm = Parallel::ForkManager->new(2);

sub {
# result from the child will be passed as 6th arg to callback
my $res = $_[5];
p $res;

for (1..3) {
$pm->start and next;
# from here and till $pm->finish child process running
# do something useful and store result in $res
my $res = { aaa => $_ };
# this will terminate child and pass $res to parent process
$pm->finish(0, $res);