Fixed race condition discovered by Intel Inspector. 

This change also matches the thread-safe version of this file in the WRF repository.