Fixed a bug on thread exiting