theknightsofni has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am having problems with the following code...this fails with any thing beyond 9 @stores.. I am not able to understand the current available Thread::Queue documentation to make sense of whats going on.... Any help is appreciated... Thanks
#!/opt/perl-5.8.8/bin/perl -w # # use strict; use Getopt::Long; use threads; use threads::shared; use Thread::Queue; require Data::Dumper; my $max_thread_count = 10; my $version = "0.1_01"; my @stores = sort map { sprintf 'store%03d', $_ } 0..$max_thread_count +; my $thread; my $thread_list; my $tid; sub writeLog { print "$_[0]\n"; } my $jobs = Thread::Queue->new(@stores); # read work file sub processWF { my @lines = ( 'perl -wle "print q(storeplaceholder stage 1);sleep(rand(10));pr +int q(storeplaceholder stage 1 done)"', 'perl -wle "print q(storeplaceholder stage 2);sleep(rand(10));pr +int q(storeplaceholder stage 2 done)"', ); while (defined (my $item = $jobs->dequeue)) { writeLog("Launching $item"); foreach my $line (@lines) { print "replacing $line with $item\n"; $line =~ s/storeplaceholder/$item/g; #print "running: $line\n"; system($line) == 0 or warn "Couldn't launch $line: $!"; } } } $jobs->enqueue(undef) for 1..$max_thread_count; my @workers = map { threads->create( \&processWF ) } 1..$max_thread_co +unt; $_->join() for @workers;

Replies are listed 'Best First'.
Re: Problem with threading code
by BrowserUk (Patriarch) on Nov 28, 2008 at 22:52 UTC
    this fails with any thing beyond 9 @stores..

    Define "fails"? Because I've just run it here with $max_th..(got bored)... that variable set to 100, and it appears to run to completion, and exactly as I would expect it to.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I am getting the following error when it gets to Thread 10: replacing perl -wle "print q(store003 stage 2);sleep(rand(10));print q(store003 stage 2 done)" with store010 store003 stage 2 Instead, it should be something like replacing perl -wle "print q(storeplaceholder stage 2);sleep(rand(10));print q(storeplaceholder stage 2 done)" with store010