Add shutdown hook to threads