(repeating what I said at PerlMonks ...)
BEGIN {
my $mutex;
sub that {
$mutex ||= APR::ThreadMutex->new( $r->pool() );
$mutex->lock();
$ENV{TZ}= ...;
...
$mutex->unlock();
}
}
But of course, lock () should happen in c'tor, and unlock () should happen in d'tor, with the exception of one-time hacks.
Update: note that there is a race condition in how $ mutex is initialized in the subroutine (two threads can call this () for the first time almost simultaneously). You will most likely want to initialize $ mutex before creating the (additional) threads, but I am unclear in the details on the “working” Apache MPM and how you easily accomplished this. If there is any code that starts early, simply saying that () from there will destroy the race.
APR:: ThreadMutex:
BEGIN {
my $mutex;
sub that {
my $autoLock= APR::ThreadMutex->autoLock( \$mutex );
...
}
}
, autoLock(), undef, , , $mutex.