Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why stdin lose after new Thread

4 views
Skip to first unread message

haomiao

unread,
Jul 26, 2008, 8:29:00 AM7/26/08
to
Hi

When I create a new thread, the main thread cannot get input from
<>.

The code is like
-----------------------------------------------
while ( $line = <STDIN> ) {
chomp $line;
execCmdLine($line);
print "pp> ";
}
------------------------------------------------
In the execCmdLine function, if $line eq 'conn', a new thread is
created. Then
the main thread can not get any line at all.

How can I let the main thread continue?

zentara

unread,
Jul 26, 2008, 11:40:40 AM7/26/08
to
On Sat, 26 Jul 2008 05:29:00 -0700 (PDT), haomiao <miao...@ustc.edu>
wrote:

You don't understand how threads in Perl work very well.

When a thread gets spawned, it gets a copy of it's parent at the time.
This means when you try to dynamically spawn threads, each previous
thread gets a copy of previous ones, and memory usage of the app tends
to increase, as the memory dosn't get cleaned up completely.

It is all very complex, and you can search groups.google.com for
"perl threads", I'm not going to try an explain all the complexities
here.
Unless you really need threads for some reason, like sharing variables,
you will be better off fork-and-exec'ing stuff, as the memory cleanup
will be better.

But....
I found that if you put the readkey in a thread, you can keep
the mainloop going while receiving input. I use the Glib event
loop system here, to make things easier.

See:
http://perlmonks.org?node_id=664462

zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html

xho...@gmail.com

unread,
Jul 27, 2008, 9:11:15 PM7/27/08
to

Show us the contents execCmdLine, suitably reduced such that it does
as little as possible but still exhibits the relevant behavior.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

0 new messages