Very Nice , There is only one unsolved issue here: Your program may exit with other threads are still running , beside this I liked it very much. The actual solution you supplied is the use of threads->self function. Thanks you very much!!! Igor BTW where can I get some documentation about threads::Pool?
-----Original Message----- From: zentara [mailto:[EMAIL PROTECTED] Sent: Monday, October 13, 2003 1:16 AM To: [EMAIL PROTECTED] Subject: Re: FW: threads in perl On Sun, 12 Oct 2003 15:21:54 +0200, [EMAIL PROTECTED] (Igor Ryaboy) wrote: >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 Just in case it may help you out, here is the snippet I came up with which seems to work with threads. I tested it in as much as I ran top and watched that only maxnum threads run at a time. #!/usr/bin/perl use warnings; use threads; #seems to work my @data = qw(a b c d e f g h i j k l m n o p); my $workernum = 4; my $finished = 'false'; my @running_threads = (); while ($finished eq "false") { @running_threads = threads->list; if (scalar(@running_threads) < $workernum) { $somedata = shift @data || undef; if (defined $somedata) { threads->new(\&worker, $somedata); } else { $finished = "true"; } } } #cleanup leftover running threads print "data exhausted, waiting for threads to finish\n"; threads->list()->join; #And this is the code for the worker: sub worker { # do whatever my $paramin = shift; print "\tchild ", threads->tid(), " created ok using param $paramin\n"; sleep(int(rand(30))); print "\tchild", threads->tid() , " done, outta here\n"; threads->self->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]