optimized thread ending