1. Volatile variable as initializer for non-volatile variable?
2. VC linker optimizations and whole program optimization
Hi all. ( Visual Studio 2005 ) We do not use any compiler optimizations (/O...) in our release builds due to the subtle bugs they may reveal and due to the fact that debugging C++ code in optimized build is a real pain (and, yes, we need to debug release builds rather too often). My real question now is with regard to the linker optimizations and whole program optimization: We have seen that enabling the following switches: compiler: (... /Od ) ... /GL linker: (... /DEBUG) ... /OPT:REF /OPT:ICF /ltcg where GL = [Enable link-time code generation] REF = [Eliminate Unreferenced Data] ICF = [Remove Redundant COMDATs] ltcg = [Use Link Time Code Generation] significantly reduces the size of the resulting binaries. (The Size difference btw. /OPT + /GL + /LCTG vs. only /OPT is about 15%) My question is now: Will the LTCG mess around and optimize something in the compiler-sense (reordering, removing code, ... etc.) or is it safe, i.e. the only optimizations performed in our case would be removal of unreferenced functions/data and some merging of template instantiations ... ?? Any insights welcome! cheers, Martin
3. Value of compiler/habitual optimization (was: optimization of static data initialization)
4. Access to volatile objects applies to non-volatile objects?
6. volatile member variable with Interlocked
I have an member variable (int) that is accessed by multiple threads using Interlocked.Increment(), Interlocked.Decrement(), and read directly. Using volatile gives me "CS0420: a reference to a volatile field will not be treated as volatile" warnings when using the Interlocked functions, which I can easily disable with "#pragma warning disable 420". Should this variable be marked volatile? Is volatile necessary in this case? Thanks! Mark -- Mark Salsbery Microsoft MVP - Visual C++
7. C# volatile field - CSharp/C#
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: Interlocked.Increment(ref count); The error says that a volatile field cannot be used as ref or out, but if I don't use the volatile field, the value may be cached away in some method that is just reading the field. Consider, for example, an integer field being used by three threads. Two threads intend to increment the value of the field while the third one just wishes to check the value. Consider, for example, the initial value of the field is 5. If both the writer threads increment the value using the interlocked API, the final value of the field is guaranteed to be 7. Without the use of the interlocked API, the final value could be 6. Therefore, using the interlocked API is a must in this case. Without the field being marked as volatile, the reader thread may always see the value as 5. As can be seen, we need both the mechanisms under some cases. What might be the solution for such situations?