Your first thread puts the socket into a blocking read state.
When you attempt to start the second thread, Perl tries to clone $s for that thread, because it closes over $s.
But, the underlying system handle for the socket is locked (by the OS) because the socket is in a blocking read; thus the attempt to clone the handle is blocked until the read completes; which it never will.
As the clone is blocked, the second thread is never started; so no GET is issued, so no reply will be received.
The solution (maybe, depending upon the rest of your application,) it to ensure the get occurs before the read state is entered.
Something like this:
use strict; use warnings; use threads; use IO::Socket::INET; $|++; my $s = IO::Socket::INET->new(PeerAddr => 'www.google.com', PeerPort => 80, Proto => 'tcp') or die "error: $@"; print "create t2\n"; my $t2 = threads->create(sub { # problem: this is never printed print "in t2\n"; $s->send("GET / HTTP/1.1\r\n\r\n"); }, 0 ); # give t1 a moment to execute and to block in the recv call print "sleep 1s\n"; sleep(1); print "sleep done\n"; print "create t1\n"; my $t1 = threads->create(sub { my ($r) = @_; my $data = ''; print "in t1\n"; while( 1 ) { $r->recv($data, 64); print ">>> $data"; } }, $s); # problem: this is never printed, as threads->create never returns print "done\n"; $t1->join(); $t2->join();
Outputs:
C:\test>junk41 create t2 in t2 sleep 1s sleep done create t1 done in t1 >>> HTTP/1.1 302 Found Cache-Control: private Content-Type: text/h>>> tml; charset=UTF-8 Location: http://www.google.co.uk/?gfe_rd=cr>>> &ei=-lgnU7OuDdT88QPQ84 +CwAw Content-Length: 261 Date: Mon, 17 M>>> ar 2014 20:20:10 GMT Server: GFE/2.0 Alternate-Protocol: 80:qu>>> ic <HTML><HEAD><meta http-equiv="content-type" content="text/>>> html;cha +rset=utf-8"> <TITLE>302 Moved</TITLE></HEAD><BODY> <H1>3>>> 02 Moved</H1> The document has moved <A HREF="http://www.google.>>> co.uk/?gfe_rd=cr&ei=-lgnU7OuDdT88QP +Q84CwAw">here</A>. </BOD>>> Y></HTML> Terminating on signal SIGINT(2)
In reply to Re: threads->create() blocks on Windows
by BrowserUk
in thread threads->create() blocks on Windows
by photron
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |