CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



mojo2405
New User

Dec 4, 2013, 10:06 PM

Post #1 of 3 (9655 views)
threads queue and memory leak Can't Post

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


Code
   #Start new thread 

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

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

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



Laurent_R
Veteran / Moderator

Dec 5, 2013, 10:49 AM

Post #2 of 3 (9610 views)
Re: [mojo2405] threads queue and memory leak [In reply to] Can't Post

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


mojo2405
New User

Dec 6, 2013, 4:19 AM

Post #3 of 3 (9559 views)
Re: [mojo2405] threads queue and memory leak [In reply to] Can't Post

problem solved using this code.


Code
use 5.010; 
use strict;
use warnings;

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

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

$pm->run_on_finish(
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);
}

$pm->wait_all_children;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives