in reply to Thread create hangs

and that is where it hangs ... Interesting thing is when main main thread prints something after I enter something on console the thirdThread is created.

Because Perl is serialising access to STDIN. Probably because it needs to clone STDIN for the new thread, and can't do that while it is in an input state. (Though I do not understand why not!)

Ie. Because your main thread has STDIN in a input state here: while(<STDIN>), Perl won't create a new thread until the input state is completed. Hence why the thread "runs" once you hit enter.

I thought for a moment that this synchronisation might be a MS CRT thing, but doing the equivalent in C, and it doesn't happen:

#include <windows.h> #include <stdio.h> void thread2( void *p ) { printf( "[%d] Running\n", GetCurrentThreadId() ); } void thread1( void *p ) { while( 1 ) { Sleep( 2000 ); printf( "[%d] About to start a new thread\n", GetCurrentThread +Id() ); _beginthread( thread2, 0, 1 ); } } void main( void ) { char buf[ 1024 ]; _beginthread( thread1, 0, 1 ); while( gets( buf ) ) { printf( "CMD: %s\n", buf ); } return; } /* output C:\test>junk2.exe [3120] About to start a new thread [5168] Running [3120] About to start a new thread [3944] Running [3120] About to start a new thread [6016] Running fred CMD: fred [3120] About to start a new thread [6996] Running [3120] About to start a new thread [6980] Running */

I think this is a bug; and I do not have an immediate workaround for it either.


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.
RIP an inspiration; A true Folk's Guy