Solves thread syncing problem.

Before this fix, the game could crash sometimes.