#!/usr/bin/perl use warnings; use strict; use threads; use IO::Select; use FileHandle; my @ranges = ( [1,10000000],[10000001,20000000],[20000001,30000000], [30000001,40000000],[40000001,50000000] ); my $sel = new IO::Select(); # thread launching foreach (@ranges){ my $fh = FileHandle->new(); open ($fh,'+>', './dummyfile'); # needed to get filehandle to give a fileno # maybe better to use IO::Handle and give it # a fileno directly? my $start = $_->[0]; my $end = $_->[1]; my $fileno = fileno($fh); print "$start $end $fileno\n"; threads->create( \&thread, $start, $end, $fileno )->detach; $sel->add($fh); } # watching thread output print "Watching\n\n"; #while( scalar (threads->list) > 0 ){ # dosn't seem to work while(1){ foreach my $h ($sel->can_read){ my $buf; if ( (sysread($h, $buf, 1024) > 0 ) ){ print "Main says: $buf\n"; #truncate $h, 0; # bad idea :-) } } } sub thread{ my( $start, $finish, $fileno ) = @_; open my $fh, ">&=$fileno" or warn $! and die; print $fh "thread# ",threads->tid()," -> $start, $finish, $fileno \n" ; sleep 5; print $fh "thread# ",threads->tid()," -> finishing \n" ; } __END__