move remove_global_thread() out of the lock

since it locks the same mutex, causing deadlock