I have a problem on win32 (64bit win 7, perl v5.20.2, active state), where threads->create() hangs.

I am looking for debug pointers.

The code launches two processes: Process (1) a background process, and process(2) is GDB, the gdb target talks to the background process via a socket, I am creating an automated sw test environment, I need to control, interact and capture output (stdout) from the processes.

I'm using IPC::Open3() to create both processes processes, like this:

# i,o,e are std in/out/err $p = open3( $i, $o, $e, @cmdline ); $hr->{'alive'} = 1; $hr->{'pid'} = $p; $hr->{'stdout'}= $o; $hr->{'stdin'} = $i; $hr->{'stderr'} = $e;
To process the stdout, I create a reader thread like this:
# The reader is given the hash reference, the 'o' # indicates this is the 'stdout' reader $hr->{'o_thread'} = threads->create( \&reader_thread, $hr, 'o' );
The above works quite well when I run one sub process. It locks up (the create call does not return) if I have two sub processes.

My only means to Debug is via print() - tells me that the call to threads->create() does not return, and print statement I put before threads->create() occurs, the after does not. And the print statement I put at the entry to the reader_thread never happens,

To be clear, this works a few times then does not work again.

Any suggestions how to dig deeper and figure out what I am doing wrong. Often, I insert print statements into various PM modules, but - threads are not perl, they are native code.

The reader thread is quite simple:

sub reader_thread { my ($hr,$who) = @_; my $c; my $h; my $r; my $q; threads->detach(); # determine handle. if( $who eq 'o' ){ $h = $hr->{'stdout'}; } else { $h = $hr->{'stderr'}; } # Get the output queue $q = $hr->{'Q'}; while( 1 ){ if( $hr->{'alive'} == 0 ){ last; } # Try to read *ONE* byte $r = read( $h, $c, 1 ); if( $r > 0 ){ if( length($c) ){ $q->enqueue($c); } } else { threads->yield(); } } }

In reply to threads->create hangs by duane_ellis2

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.