Hi again, I figured out a way with pipes. It uses IO::Pipe and is a bit tricky, because you need to pass the IO::Pipe object to the thread, before you call writer or reader on it. Otherwise IO::Pipe throws an error about IO::Pipe::End. But this works as proof of concept. It does not have any disk files involved to avoid the fileno problem.
Also, since I detached the threads, you need to hit control-c to exit, or work out a method of detecting when all threads are finished, probably thru a shared variable.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use IO::Select;
use IO::Pipe;
my @ranges = ( [1,10000000],[10000001,20000000],[20000001,30000000],
[30000001,40000000],[40000001,50000000] );
my $sel = new IO::Select();
# thread launching
foreach (@ranges){
my $pipe = IO::Pipe->new();
my $start = $_->[0];
my $end = $_->[1];
print "$start $end $pipe\n";
threads->create( \&thread, $start, $end, $pipe )->detach;
# only call reader after pipe has been passed to thread
$sel->add( $pipe->reader() );
}
# watching thread output
print "Watching\n\n";
while(1){
foreach my $h ($sel->can_read){
my $buf;
if ( (sysread($h, $buf, 1024) > 0 ) ){
print "Main says: $buf\n";
}
}
}
sub thread{
my( $start, $finish, $pipe ) = @_;
my $wh = $pipe->writer;
$wh->autoflush(1);
print $wh "thread# ",threads->tid()," -> $start, $finish, $pipe \n"
+;
sleep 5;
print $wh "thread# ",threads->tid()," -> finishing \n" ;
sleep 2;
}
__END__
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.