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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: hoto start 50 threads and keep 50 threads running ?: Edit Log

New User

Aug 25, 2011, 6:41 AM

Views: 634
hoto start 50 threads and keep 50 threads running ?

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;
print "$var\n";
$thr[$count] = new Thread \&sample_sub;


sub sample_sub {
print time;
sleep 1;

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.


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

Edit Log:
Post edited by xforze (New User) 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