#! perl -slw use strict; use IO::Socket; use threads qw[ yield ]; use threads::shared; use Thread::Queue; $| = 1; our $THREADS ||= 5; my $listening : shared = 0; our $ios = IO::Socket::INET->new( LocalPort => 6969, Type => &IO::Socket::SOCK_STREAM, Proto => 'tcp', Reuse => 1, Listen => 100, ) or die "IO::S->new failed with $!"; print "$ios"; sub server { $listening++; my( $Qquery, $Qreply ) = @_; my $tid = threads->self->tid; print "tid:$tid"; ## Give th other threads a chance to get up and running. yield until $listening == $THREADS; while( my $client = $ios->accept() ) { chomp( my $query = <$client> ); # print "$tid: $client got: '$query'"; $Qquery->enqueue( "$tid:$query" ); my $reply = $Qreply->dequeue(); print $client $reply; close $client; } $listening--; } my @Qs = map{ new Thread::Queue } 0 .. $THREADS; threads->new( \&server, $Qs[ 0 ], $Qs[ $_ ] )->detach for 1 .. $THREADS; yield until $listening == $THREADS; print "Threads $listening running; grabbing data"; open BIGFILE, '< :raw', 'data/50MB.dat' or die "data/50mb.dat: $!"; my $data; sysread( BIGFILE, $data, -s( BIGFILE ) ) or die "sysread BIGFILE : $!"; close BIGFILE; while( $listening ) { my( $tid, $msg ) = split ':', $Qs[ 0 ]->dequeue(); ## Process request print "Received '$msg' from $tid"; $Qs[ $tid ]->enqueue( 'Thankyou for your enquiry' ); }