An attempt to use thread locking to prevent race condition.