Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Threads

by PH (Initiate)
on Jun 06, 2001 at 17:53 UTC ( #86203=perlquestion: print w/ replies, xml ) Need Help??
PH has asked for the wisdom of the Perl Monks concerning the following question:

Do threads work properly under WinNT? The perldoc says creating a new Thread like this:-
use Thread; $thread = new Thread \&sub_name;
Creates the thread and continues execution, but when I try this it runs the sub (sub_name) but then it blocks. There is a 1 second sleep in the thread sub (Which is a never ending for loop!)
sub sub_name() { for(;;) { // do stuff printf("."); sleep(1000); } }
This prints a "." every 1 second, but does not allow any interaction from the user or from anything!!! Thanks in advance, Paullo

Comment on Threads
Select or Download Code
Re: Threads
by Eradicatore (Monk) on Jun 06, 2001 at 18:38 UTC
    I know you're asking about threads, but from what I've experienced and researched so far on win32 and NT is that its easiest maybe to use fork. From what I can tell, fork on win32 is like a hybrid between a true fork and a thread. It doesn't create a new seperate process as seen by the task manager, but it also does not have access to global variables in the parent like a thread should.

    My example shows how to use fork together with Tk to get web pages but not interfere with the gui. The child just sits there waiting for urls to get. You will have to add proxy support if you're behind a firewall though.

    Oh, also, you want to make sure you download the right version of perl that supports fork. You can get this from active state at here

    #!/usr/local/bin/perl use POSIX; use Tk; use LWP::UserAgent; use URI::URL; pipe(FROM_P, TO_C) or die "pipe: $!"; select(((select(TO_C), $| = 1))[0]); my $ua = LWP::UserAgent->new; if (!($pid = fork)) { close(TO_C); while($line = <FROM_P>) { print "Child Pid $$ just read this: $line\n"; chomp($line); $url = $line; $file = 'yahoo.txt'; my $req = HTTP::Request->new(GET => $url); $req->header('Accept' => 'text/html'); $res = $ua->request($req, $file); }; } my $mw = MainWindow->new(); $button = $mw->Button(-text => "click here to download yahoo web page" +, -activebackground => 'red', -command => \&printit)->pack(); print "i'm the parent about to mainloop\n"; MainLoop; sub printit { print TO_C "http:\/\/www\.yahoo\.com\n"; }

    Justin Eltoft

    "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Re: Threads
by Anonymous Monk on Jun 06, 2001 at 19:02 UTC
    Wait 1000 seconds (approx. 20 min) and see if it blocks then.
      Yeah, 'sleep' waits in seconds, not milliseconds. ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://86203]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-07-28 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (200 votes), past polls