http://qs321.pair.com?node_id=632320

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

I humbly seek wisdom.

I have a multi-threaded program using Thread::Queue to pass reults to the next thread. Main drops $href = &share({}); onto the first (zeroth?) queue. Each thread takes off the previous queue, produces as many $outSolutionref = &share({}); as necessary and puts them on the next queue.

So far, so good. In fact so good I could stop (but what would be the fun in that?).

Now I'm thinking of writing status out while it's working instead of only when it's done, so I'm using Win32::Console to write out the count of results taken off the input queue and written to the output queue for each thread:

#/usr/bin/perl -w use strict; use threads; use threads::shared; use Thread::Queue; use Win32::Console; my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE); my $CURRENT_ROW = 3; my @consoleInfo = $CONSOLE->Info(); my $statusRow = $consoleInfo[$CURRENT_ROW]; my $numthreads = 4; my @DataQueues; #There are n+1 queues (mileposts vs. miles). for ($i = 0; $i <= $numthreads + 1; $i++) { $DataQueues[$i] = Thread::Queue->new; } my @aThreads; for ($i=0; $i <= $numthreads; $i++) { $aThreads[$i] = threads->new(\&threadprocessor, $DataQueues[$i], $D +ataQueues[$i+1], $i ); } #put first (empty) solution on the first queue my $hrefSolution = &share({}); $DataQueues[0]->enqueue($hrefSolution); $DataQueues[0]->enqueue(undef); #wait for threads to exit. for ($i=0; $i<=$#aThreads; $i++) { $aThreads[$i]->join; } #dequeue from the last queue of the chain my $Solutionref; my %Solution; print "\nresults: \n"; while ($Solutionref = $DataQueues[$#DataQueues]->dequeue) { %Solution = %{$Solutionref}; printSolution (\%Solution); } print "\n"; sub printSolution { my $href = shift; print "\nKEYS: "; print(($_).' ') foreach (sort keys %{$href}); print "\nvals: "; print(($$href{$_}).' ') foreach (sort keys %{$href}); print "\n"; } sub threadprocessor { my ($inqueue, $outqueue, $threadNum) = @_; my $inSolnQty = 0; my $outSolnQty = 0; my $statString; my $threadStatusColumn = $threadNum * 12; my $inSolutionref; my %inSolution; my $outSolutionref; my %outSolution; while ($inSolutionref = $inqueue->dequeue) { $statString = sprintf ("%5s/%-5s", $inPattQty++, $outPattQty); $CONSOLE->WriteChar($statString, $threadStatusColumn, $statusR +ow); %inSolution = %{$inSolutionref}; While (<>) #ok, I'm hiding complexity here if (isSolution($_)) { #ok, I'm hiding complexity here $outSolutionref = &share({}); %$outSolutionref = (%inSolution); #ok, I'm hiding complexity here $outqueue->enqueue($outSolutionref); $statString = sprintf ("%5s/%-5s", $inSolnQty, $outSol +nQty++); $threadCONSOLE->WriteChar($statString, $threadStatusCo +lumn, $statusRow); } } } $outqueue->enqueue(undef); $statString = sprintf ("%5s/%-5s.", $inSolnQty, $outSolnQty); $threadCONSOLE->WriteChar($statString, $threadStatusColumn, $statu +sRow) }

The problem being, after the first queue ends (as indicated by the period) all output stops. Do I need to share the $CONSOLE? or something? What documentation should I have read?