add flag to stop thread