Use a latch instead of a conditionvariable to avoid a race