Fix compilation errors on Windows

Fix many compilation errors on Windows, following the latest "Thread Safety Improvements"