Thread: Use atexit to run a function on exit.
Thread: Refactor run_queued to be more responsive.