Here's the rewrite of this patch to use cleanups and refcounting on the thread_info objects to prevent them from being deleted while the thread is held in a cleanup. This version completelly drops the external/internal thread separation, and sticks with inferior_ptid to mean current thread. Exited threads are kept in the thread list until safe to delete. Most commands aren't allowed while the user has an exited thread selected. Tested on x86_64-unknown-linux-gnu -m32, sync/async. -- Pedro Alves