in reply to Re: Reading from file in threaded code is slow
in thread Reading from file in threaded code is slow
It looks to me like your threaded version has a single process that's trying to open the same file 4 times and read from 4 different file positions?
In the routine thread_process(), the list of files is pushed onto a queue. Each of the threads then pull (different) filenames from that queue:
sub thread_process { my $args = shift; my @threads; my $queue = Thread::Queue->new(); for my $p ( 1 .. $PARALLEL ) { push @threads, threads->create( sub { while( my $file = $queue->dequeue) { process($file, "Thread $p of $PARALLEL"); }}) } $queue->enqueue( @$args, (undef) x $PARALLEL ); $_->join for @threads; return 1; }
So no. They are not reading from the same file.
|
|---|