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?


In reply to multi-threaded win32::console by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.