This is useful, thanks,  but:
I need in general to maintain a constant/fixed number of threads running i.e. when one 
is finished other will be started but I am not allowed to start more than x threads.
How can I do it?
I've tried to do it based on threads->list but the problem that I don't know which 
thread should I join.
Thanks
Igor

-----Original Message-----
From: zentara [mailto:[EMAIL PROTECTED]
Sent: Sunday, October 12, 2003 2:47 PM
To: [EMAIL PROTECTED]
Subject: Re: threads in perl


On Sun, 12 Oct 2003 08:24:52 +0200, [EMAIL PROTECTED] (Igor
Ryaboy) wrote:

>I'll be more specific:
>I need to open a lot of threads with same name when passing different parameters to 
>each. 
>When inside each thread there is a system call which can take a lot of time. I want 
>that after 
>the system call ends the thread will close itself and remove itself from 
>threads->list. 
>For example:

>while(...) #here will be some condition concerned with currently running threads i.e 
>threads->list 
>    $thr=new threads \&create_thread, $param; 
>    $param=$param+1;
>
>sub create_thread {
>    print("\nNew thread was started\n");
>    my @create_param = @_;
>    system("...");
>    print ("\nOld thread was finished\n");
>}
>
>Unfortunately this construction didn't worked because the threads->list didn't 
>decremented by itself after the system call was finished?
>What should I do? Maybe use of "join" in some way? 

Bear in mind I'm not a "threading expert", just a hacker with ideas. :-)
Here is a snippet to try. I used a global for $param instead of passing
it, but you may be able to pass the $param.  Also maybe you should check
out the module Thread::Pool, which manages threads and tasks.

#!/usr/bin/perl
use threads;
my @kiddies;
my $param =10;

while ($param > 0){
  print "$$ starting loop $_";
  push @kiddies, threads->new(\&sub1);
  print "$$ exiting loop $_\n";
  $param--; 
 }

sub sub1{
 print "\tchild ", threads->tid(), " created ok using param  $param\n";
 sleep(int(rand(10)));
 print "\tchild", threads->tid() , " done, outta here\n";
}

foreach (@kiddies){ $_->join(); }
__END__




Our body's 20 milligrams of beta radioactive Potassium 40
emit about 340 million neutrinos per day, which go at
lightspeed to the ends of the universe!..even thru the earth. 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to