I am trying to utilize threads and non-blocking IO but have run into a stumbling block. I have a separate thread monitoring STDIN via Term::Readkey. After some time I create another thread to do non-related processing but this thread hangs at creation time until something happens (i.e. a key is pressed) in the first thread (read_in). Am I creating threads incorrectly or is there something else blocking the proc_it thread below? Code was modified from a nonblocking IO post by zentara. This code will hang at count = 10 until something is read from the command prompt. How do I avoid this block?
#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; use threads; $|++; ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $count = 0; my $thr = threads->new(\&read_in)->detach; while(1){ print "test, count:$count\n"; sleep 1; if ($count == 10) { my $thr_proc = threads->new(\&proc_it)->detach; } $count++; } ReadMode('normal'); # restore normal tty settings sub proc_it{ while(1) { print "@"; sleep 2; } } sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; if($char eq 'q'){exit} } } } __END__

In reply to Non-blocking IO and threads by PhillyR

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.