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&amp;ei=-lgnU7OuDdT88QP +Q84CwAw">here</A>. </BOD>>> Y></HTML> Terminating on signal SIGINT(2)

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

In reply to Re: threads->create() blocks on Windows by BrowserUk
in thread threads->create() blocks on Windows by photron

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.