I have a list of data that I want to operate concurrently on, as many threads as I have processors. So I wrote a bit of code that uses a semaphore to halt spawning of new threads when the limit are already running.

I have two problems with the following code. First of all, it "dead"blocks on '$available->down()', since the thread apparently gets a copy of the semaphore (I've tried 'my $available :shared=...' and variations, with no change). How can I get my threads to up() the semaphore before returning?

Secondly, after each thread returns I get a "Scalars leaked: 1" internal Perl error on the console. Is this a sign that I'm doing something unwelcome? How do I get it to stop leaking scalars?


thanks,
-natevw

==========

#!/usr/bin/perl

use warnings;
use Thread qw(async);
use Thread::Semaphore;
use Data::Dumper;

sub tothread {
print "$_[0]\n";
sleep(1);
}

doThreads(\&tothread,[10,9,8,7,6,5,4,3,2,1]);

sub doThreads {
my ($function,$values)[EMAIL PROTECTED];

my $processors=4;
my $available=new Thread::Semaphore($processors);
foreach my $val (@$values) {
 $available->down();
my $t = async { $function->($val); $available->up(); print "$available is ",Dumper($available); };
 $t->detach();
}
for (my $i=0; $i<$processors; $i++) { $available->down(); }
}


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to