thread safely exit