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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |