PR threads/13217 * thread.c (thread_apply_all_command): Check for valid threads and thread count. (thread_array_cleanup): New struct. (set_thread_refcount): New function.