Fix race condition for multithreaded code.