Use condition variables to reduce cpu load