It's not a "race condition"! (And it not "<> blocking thread creation".)

You cannot clone the current thread whilst another thread has a (Perl internal) lock on one of the resources to be cloned. This is WAD (Working As Designed). Perl protects it's internal data structures from concurrency corruption by serialising access to them through an internal mutex. If one of the process global resources (PGR) of a spawning thread is currently in use by another thread then the cloning of the current thread will be blocked until that mutex is released. In this case, the shared PGR is STDIN--but it could be any other PGR that blocks.

In the following, if you supply an argument, STDIN will be closed in the main thread, hence it does not need to be cloned in order for thread 2 through 11 to be spawned, and the program will complete immediately.

If you do not supply an argument, STDIN is not closed, and when the attempt is made to clone the main thread for thread 2, the internal mutex is being held by thread 1 because it is in a blocking read state on STDIN. Therefore, the main thread is blocked until that read state is satisfied.

If you hit enter, the read on STDIN in thread 1 returns, lifting the mutex and allowing the main thread to continue. A few threads will be created before thread 1 gets another timeslice, and again enters a blocking read state whilst holding the internal mutex. The main thread is once again blocked. Hit enter again and the cycle repeats.

See the two sample runs after the __END__ token:

#! perl -slw use strict; use threads; async { getc while 1; }->detach; close STDIN if @ARGV; for ( 1 .. 10 ) { async { printf "Thread: %d ran\n", threads->tid; }->detach; } sleep 1 while threads->list( threads::running ); __END__ C:\test>junk Thread: 2 ran Thread: 3 ran Thread: 4 ran Thread: 5 ran Thread: 6 ran Thread: 7 ran Thread: 8 ran Thread: 9 ran Thread: 10 ran C:\test>junk 1 Thread: 2 ran Thread: 3 ran Thread: 4 ran Thread: 5 ran Thread: 6 ran Thread: 7 ran Thread: 8 ran Thread: 9 ran

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^8: threads on Windows by BrowserUk
in thread threads on Windows by kennethk

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.