You can use a lock instead of a boolean value to do this check.