Hi!

I have a problem with using threads and sockets on Windows: threads->create() blocks. The following code is a simplified part of a bigger project in which I found this problem:

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 t1\n"; my $t1 = threads->create(sub { my ($r) = @_; my $data = ''; print "in t1\n"; while (1) { $r->recv($data, 64); #print ">>> $data\n"; } }, $s); # give t1 a moment to execute and to block in the recv call print "sleep 3s\n"; sleep(3); print "sleep done\n"; 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); # problem: this is never printed, as threads->create never returns print "done\n"; $t1->join(); $t2->join();

The code opens a socket to www.google.com. Google is just used for demonstration purpose. The actual project connects to a special server using a simple request/response protocol. Then thread t1 is created to receive all incoming data. After that I injected a sleep(3) to ensure that t1 is blocking in the recv() call before going on. This is important, the problem of threads->create() hanging only occurs if t1 is blocking in recv(). Finally t2 is created and should send a request. But this never happens on Windows, because threads->create() for t2 never returns.

This problem only occurs on Windows (tested with Strawberry and Active Perl 5.18.2). It doesn't occur on Linux, there this just works as expected.

Am I doing something wrong here? Is there some general problem with this approach that could explain the problem? Or is this a bug in perl itself?


In reply to 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.