Work around mingw bug thread_local bug https://sourceforge.net/p/mingw-w64/bugs/527/ that causes failures in windows "test cross-built" CI jobs in https://github.com/bitcoin/bitcoin/pull/32387 by skipping thread_local destructors in mingw.
The workaround will result in resource leaks in mingw builds, that might be noticeable if a lot of threads are created and destroyed, but should not be a significant problem in practice.
This change is just a refactoring in other builds (including MSVC) not affected by this bug.