Thanks to some help, I've started attempting to get thread-safe logging working for my script - however, I don't seem to have it working correctly:
use Fcntl ':flock';
no warnings 'redefine';
sub IO::Tee::PRINT
{
my $self = shift;
my $ret = 1;
foreach my $fh (@$self) {
flock($fh, LOCK_EX);
print "\n\t\ttestA\n"; #<-- added for testing
undef $ret unless print $fh @_;
flock($fh, LOCK_UN);
print "\t\ttestB\n"; #<-- added for testing
}
return $ret;
}
my $Info_tee = IO::Tee->new(\*STDOUT, ">$ENV{DOM}\\build.log");
When I get to the threaded section of my script:
print $Info_tee "\n------------------------------------------------------\n";
print $Info_tee "\n\t\t*** Performing Action: \'$cmd\' on $comp ***";
My output to STDOUT
(4 threads) is:
testA
testA
------------------------------------------------------
testB
testA
------------------------------------------------------
testB
------------------------------------------------------
testB
testA
testA
------------------------------------------------------
testB
.. and then the script locks up. What am I doing wrong?
Edit: I've created a simple example of my issue here -- I noticed that if you remove the queue from the script, everything seems to work as designed.