Use condition variables correctly