It is possible to share a filehandle from multiple threads (but there may be caveats). The problem is how to pass the handle (an object in Perl's terms) to the thread that is going to perform the further processing as iTthreads won't allow you to share objects.
The solution is to pass the fileno associated with the filehandle and then "dup" the handle within the thread using the syntax open my $newhandle, "<&=$fileno" ... Note:That is "<&=$fileno" (an alias file descriptor) not "<&$fileno" (a duplicate file descriptor) (see perlopentut for details). Note: Although this uses the open built-in syntax, it is not reopening the file, mearly duplicating the internal control structures required for accessing the existing open filehandle.
This incomplete demo, open files in the main thread, prints the first 10 lines before passing the fileno for that filehandle to a newly created thread that prints the rest of the file, closes it and dies:
#! perl -slw
use strict;
use threads;
use threads::shared;
sub thread{
my( $fileno ) = @_;
open my $fh, "<&=$fileno" or warn $! and die;
printf "%d:%s", threads->self->tid, $_ while defined( $_ = <$fh> )
+;
close $fh;
}
for my $file ( map{ glob $_ } @ARGV ) {
open my $fh, '<', $file or warn "$file : $!" and next;
printf "From main: %s", scalar <$fh> for 1 .. 10;
printf "Fileno:%d\n", fileno $fh;
threads->create( \&thread, fileno( $fh ) )->detach;
printf 'paused:';<STDIN>;
}
This seems to work for simple filehandles and sockets, but I see no reason (but have not verified) why it should not also work for those created using Filehandle, just use the fileno method.
The one caveat I am aware of is that I have occasionally seen strange effects when trying this with files opened for read-write access, but it is transient and usually "goes away". That is to say, I've only seen it occasionally and it has always appeared to fix itself when other bugs where resolved, but I have done very limited testing with this. If you wish to pursue this, you will be a pioneer. Feedback welcomed.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
|