One way would be to add a flag to your thread that it monitors, once the flag is set, you can have your thread terminate gracefully, cleaning up after it self.