Added lock for thread access to shared data structures.