thread: Add a function to pop and call all registered cleanup handlers