If you are still implementing threads: Don't forget to use flock while writing to file inside the threads, something like this:
use threads;
use Fcntl qw(:flock SEEK_END);
if ($end >= $ref->{start} && $end <= $ref->{end}) {
lock($out_fh);
say $out_fh join("\t", $ID, $strand, $chr, $start, $end, $sequence
+, $numPositions, $mismatches||"", $ref->{info});
unlock($out_fh);
}
sub lock {
my ($fh) = @_;
flock($fh, LOCK_EX) or die $!;
seek($fh, 0, SEEK_END) or die $!;
}
sub unlock {
my ($fh) = @_;
flock($fh, LOCK_UN) or die $!;
}
Alternatively, use shared objects (collect it in an array or hash) and write it down later.