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: Beginner:
hoto start 50 threads and keep 50 threads running ?

 



xforze
New User

Aug 25, 2011, 6:41 AM

Post #1 of 1 (296 views)
hoto start 50 threads and keep 50 threads running ? Can't Post

Hi community,

I need help with the following perl script.
The script should run a subroutine 50 times in a thread.
When a thread is finished, a new one should be started.

I got the following code running but it crashes after a while and I see the used memory increasing:

#!/usr/bin/perl -w

use Thread qw(yield);
use threads::shared;
use Time::HiRes qw(usleep nanosleep);

my $var :shared;
my $count=0;
while(1){
print "$var\n";
if($var<50){
$thr[$count] = new Thread \&sample_sub;
}
$count++;
usleep(100000);

}


sub sample_sub {
$var++;
print time;
sleep 1;
$var--;
exit;
}


The script should work like following:

A global var is set to 0
when the subroutine is started, the global var is increased by one and decreased by one when its finished.
A subroutine is only started when the globel var (the running threads) is below 50.

greets,
tom


(This post was edited by xforze on Aug 25, 2011, 7:43 AM)

 
 


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

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