Hey all, sorry if this is a silly question in anyway, fairly new to perl. I'm using perl 5.8.8 and don't really have much freedom to upgrade to 5.10 -- I'm running under linux (a slightly customized fc 6 build; based on the 2.6.22.9 kernel

I'm in the undesirable position of putting a wrapper around a set of binaries (these are tests for racks(s) of equipment) that someone else has created in order to execute all of these binaries, parse the output, etc and provide a nice unified user view of the results.

I initally tried doing this with fork and pipe, but found that the performance of the pipes was fairly poor (i.e. stdout would get bogged down and some tests would fail due to timeouts).

I have now moved to a thread / Thread::Queue implementation, and performance is much better, but every now and again (not all the time), as the main script exits, i get:
A thread exited while 2 threads were running.
or
A thread exited while 2 threads were running. panic: MUTEX_LOCK (22) [op.c:352] during global destruction.
or something along those lines (sometimes i get a segfault for good measure as well).
Here's a snipped of the code that actually launches the threads.
my $thread = async { my $tid = threads->tid(); my ( $out, $in ); chdir $wd or die "Can't cd to $wd: $!\n"; my $pid = open2( $out, $in, $cmd ); # die "$!\n"; print $in "$_\n" foreach (@input); close $in or warn "close of input: $! $?\n"; my $err = 0; while (<$out>) { chomp; $err = 1 if (/^thread failed/); $Q->enqueue("$tid:$uut:$test:$_"); last if ($err); } kill( 2, $pid ) if $err; close $out; $Q->enqueue("$tid:$uut:$test:ENDEND"); waitpid ($pid, 0); }; $thread->detach();
then in the main script (thread 0), i sit around and pull on the queue.
my $tcount = count of tests that are running. while (1) { if ( $Q->pending ) { $_ = $Q->dequeue; chomp; if (/ENDEND$/) { $done++; print "\t-->got $done/$t_count $_\n"; } last if ( $done == $t_count ); next if (/ENDEND$/); } }

So, i would have thought that since i only send myself the ENDEND message once the open2 is done, and i make sure that i match up all of the tests i started with an end each that I would be okay to exit from the main program cleanly, but this is not so....

is there anyway that i can explicitly kill the threads? reading around I see the suggestion from the CPAN Threads module that maybe one should just turn these warnings off, but that doesn't seem right...

Any thoughts or ideas on how i can more likely have clean exits every time around?


In reply to cleanly exiting threads by JoeKamel

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.