The typical solution to a race condition is to ensure that your program has exclusive rights to something while it's manipulating it, such as a file, device, object, or variable.
Instead of continuously using PWD and hostname, you can achieve the same effect by manipulating the prompt variable to reflect the current working directory and hostname.