implement thread removal mechanism

reworked from Shih-Po Hung <arcbbb@gmail.com>