Replace sleep with cond_wait