Writing a log file with threads
You can atomically write to a file if you are working with a threadsafe queue.

#!/usr/bin/env perl

use warnings FATAL =&gt; 'all';
use strict;
use Time::HiRes qw(time nanosleep);
use threads;
use Thread::Queue;

my $logQ = Thread::Queue-&gt;new();

my $consumer = threads-&gt;new(\&amp;_logConsumer);

my @producer;
push(@producer, threads-&gt;new(\&amp;_logProducer)) for (1..10);
$_-&gt;join() foreach @producer;

$logQ-&gt;enqueue('STOP');

$consumer-&gt;join();

sub _logProducer {

  for (1..10) {
    $logQ-&gt;enqueue(sprintf('[%-17s:%04i]', time(), threads-&gt;tid()));
    nanosleep(int(rand(10_000)));
  }

  return;
}

sub _logConsumer {

  open(my $fh, '&gt;', '/tmp/atomic.log') 
     or die 'Cannot open log file, stopped';

  my $running = 1;
  while($running) {
    if (defined(my $item = $logQ-&gt;dequeue())) {
      $running = undef if $item eq 'STOP';
      print $fh "$item\n";
    }
  }
  close($fh);

  return;
}


The output of /tmp/atomic.log looks like:

[1526568957.83079 :0011]
[1526568957.8308  :0006]
[1526568957.83081 :0009]
[1526568957.83082 :0010]
[1526568957.83087 :0008]