#! perl -slw use strict; use threads; use Thread::Queue; sub getIt { require IO::Socket::INET; my $tid = threads->self->tid; my( $Q ) = @_; my $data; for( 1 .. 5 ) { my $c = IO::Socket::INET->new( 'localhost:9999' ); ## Connect print $c 'Give me 32 bytes please'; ## Ask chomp( $data = <$c> ); ## get $c->close; ## close $Q->enqueue( "$tid:$data" ); ## Queue sleep int rand( 5 ); } $Q->enqueue( undef ); ## Signal thread done } my $Q = new Thread::Queue; my @threads = map{ threads->create( \&getIt, $Q ) } 1 .. 5; for( 1 .. 5 ) { ## Handle 5 threads dying while( my $in = $Q->dequeue() ) { ## Get data my( $tid, $data ) = split ':', $in; ## Separate who from what print "Client: Got '$data' from thread:$tid"; } } $_->join for @threads; ## Wait for threads to die