in reply to Can I/O operations on the same IO::Socket be executed in different threads?

This should be portable (works on window ) .... dunno if its better or worse than using Thread::Queue for ipc( qin/qout)

#!/usr/bin/perl -- use strict; use warnings; use threads stack_size => 4096; use IO::Socket::INET; our $host = "localhost:".int(rand 99999); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $server = IO::Socket::INET->new( Proto => "tcp", LocalHost => $host, Listen => 1, Reuse => 1, ) or die "Cannot start tcp server: $!\n $@\n "; print "listening on $host\n"; sleep 1; my $client = $server->accept(); while(<$client>){ print "reader($tid) $_"; last if /exit/i; } }); async( sub { my $tid = threads->tid; use Time::HiRes qw/ usleep /; my $socket = IO::Socket::INET->new( Proto => "tcp", PeerHost => $host, ) or die "Cannot start client : $!\n $@\n "; for(1..20){ usleep 2000*rand(500); # 2000 sleep for 2 millisecond $socket->print( "writer($tid) ".scalar(gmtime)."\n" ); } $socket->print( "writer($tid) ".scalar(gmtime)." exit\n" ); }); $_->join for threads->list; __END__
  • Comment on Re: Can I/O operations on the same IO::Socket be executed in different threads?
  • Download Code

Replies are listed 'Best First'.
Re^2: Can I/O operations on the same IO::Socket be executed in different threads?
by Anonymous Monk on May 14, 2015 at 00:12 UTC

    And? You've got two connected endpoints, two descriptors. There's no shared socket.

      And? You've got two connected endpoints, two descriptors. There's no shared socket.

      Yes, that is the point of this example -- it does seem to miss the point of the OP, so yeah :)