I'm trying to use the example on page 461 of Programming Perl. I've modified
the code slightly and the problem is that the dequeue function seems to have
no effect. I never see the print statement execute that indicates it has
removed something from the queue. I do see the "New Thread starting"
message, however.

 

The async statement works too.

 

Can someone tell me why I cannot dequeue?

 

Thanks,

 

Siegfried

 

 

use Thread::Queue;

use Thread 'async';

my $cThreads = 4;

my $nThreads = 0;

 

my @Q;

my @aThreads;

 

foreach (0..$cThreads-1){

  $Q[$_] = Thread::Queue->new();

  $aThreads[$_] = Thread->new( sub {

                                my $id = shift;

                                my $sName;

                                print __LINE__. " New thread starting up id
= $id\n";

                                while (defined( $sName = $Q[$id]->dequeue)){

                                  print __LINE__. " Remove from queue $id
dequeue $sName  \n"; # this line never prints!

                                  &PopulateIndustryCompanyJobs($sName);

                                } # while

                              }, # sub

                             $_);

} # foreach

 

 

# This works!

     print __LINE__." async ". async {
&PopulateIndustryCompanyJobs($sIndustryName); }. "\n";

 

     # This would be redundant if it worked:

     $nThreads++; $nThreads %= $cThreads;

     print __LINE__. " Queue $sIndustryName on $nThreads\n";

     $Q[$nThreads]->enqueue($sIndustryName);

     print __LINE__. " $_ is ".$_->tid()."\n" for Thread->list();

 

 

Reply via email to