Revert "Remove code which is not thread-safe. (#4454)" (#4712)
* Revert "Remove code which is not thread-safe. (#4454)"
This reverts commit 5222b2c6c0366033aaa78b95b2a0a49984984203.
* Resolve race condition
* More thread-safe changes
* Remove unused lock
Polish comments