the condition variable wait must be in a loop.