<p>I need to do some simple timezone calculation in mod_perl.  DateTime isn't an option.  What I need to do is easily accomplished by setting $ENV{TZ} and using localtime and POSIX::mktime, but under a threaded MPM, I'd need to make sure only one thread at a time was mucking with the environment.  (I'm not concerned about other uses of localtime, etc.)</p>

<p>How can I use a mutex or other locking strategy to serialize (in the non-marshalling sense) access to the environment?  The <a href="http://perl.apache.org/docs/2.0/api/APR/ThreadMutex.html" rel="nofollow noreferrer">docs</a> I've looked at don't explain well enough how I would create a mutex for just this use.  Maybe there's something I'm just not getting about how you create mutexes in general.</p>

<p>Update: yes, I am aware of the need for using Env::C to set TZ.</p>

If you're using apache 1.3, then you shouldn't need to resort to mutexes.  Apache 1.3 spawns of a number of worker processes, and each worker executes a single thread.  In this case, you can write:
```
```
{
    local $ENV{TZ} = whatever_I_need_it_to_be();

    # Do calculations here.
}
```
```
Changing the variable with
```
local
```
means that it reverts back to the previous value at the end of the block, but is still passed into any subroutine calls made from within that block.  It's almost certainly what you want.  Since each process has its own independent environment, you won't be changing the environment of other processes using this technique.
For apache 2, I don't know what model it uses with regards to forks and threads.  If it keeps the same approach of forking off processes and having a single thread each, you're fine.
If apache 2 uses honest to goodness real threads, then that's outside my area of detailed knowledge, but I hope another lovely stackoverflow person can provide assistance.
All the very best,
```
```
Paul
```
```