Support c++ compiler flags for msvc windows

/Zc : threadSafeInit

Hi and welcome to the Rust internals forum. It’s usually easier to communicate if you are a bit more elaborate/verbose when creating a topic. We don’t want to have to guess what you’re asking about or trying to suggest, so please consider writing a few sentences to express yourself more clearly.

4 Likes

This option doesn't make any sense for Rust. It changes the behavior of a specific C++ feature that Rust doesn't even have at all. In Rust all statics are initialized at compile time and do not run any constructor when starting the program or using them for the first time.

The /Zc:threadSafeInit compiler option tells the compiler to initialize static local (function scope) variables in a thread-safe way, eliminating the need for manual synchronization. Only initialization is thread-safe. Use and modification of static local variables by multiple threads must still be manually synchronized. This option is available starting in Visual Studio 2015. By default, Visual Studio enables this option.

If you want a static that is initialized using a given function when it is first accessed, you can use the lazy_static crate which is already thread safe.

If the reason for the request is that Visual Studio is trying to pass /Zc:threadSafeInit to rustc and/or cargo, that's a bug in Visual Studio's Rust support; it should be fixed in Visual Studio rather than adding compatibility switches.

If the reason for the request is that you are trying to combine C++ and Rust code into a single program and the code Rust is generating is, somehow, not compatible with the code generated by Microsoft's C++ compilers in /Zc:threadSafeInit mode, that is something to be fixed in Rust, but we're going to need a lot more detail about what you're doing and what the incompatibility is.