Wait the thread to exit before returning