What's the difference of the usage of volatile between C/C++ and C#/Java?

后端 未结 3 511
盖世英雄少女心
盖世英雄少女心 2020-12-23 18:10

I found it in many references which mention that volatile in C/C++ is is weak and may cause issue in concurrent environment on multiple processor, but it (

相关标签:
3条回答
  • 2020-12-23 18:33

    The volatile keyword is highly subjective to the language and the platform it is implemented on. While Java provides a consistent behaviour of volatile across all architectures, this is not the case for languages that are directly compiled into the native machine platform, such as in the case of C/C++. Let's try to understand why this is the case.

    Let a, b be member of a set of program actions P, and v_{n} (a) be function that applies the volatility requirement to an action, where the subscript _n denotes the _n-th iteration in which a volatile action is applied, and \rightarrow be the precedes operator, which is explained previously. For all program actions, the following rules holds:

    v_n(a) \rightarrow v_{n+1}(a)

    a \rightarrow v_n(b) \Rightarrow a \rightarrow v_{n+i}(b) where i \in \mathbb{N}

    Rule 1 says that all volatile functions enforce a total order, where the function v_{n} (a) always precedes v_{n+1} (a), where rule 2 says that if an action a precedes a volatile function on an action b on the _n-th iteration, then the action a must necessarily precede all subsequent volatile functions applied to b.

    This is a very strong memory requirement in Java, in fact it is much stronger than compared to C/C++. The C/C++ language specification has no such restriction on memory ordering, and leaves it to the compiler implementation to decide how non-volatile actions are ordered around volatile actions.

    Let's consider how the these rules affect program execution with a simple code sample:

    int a = 0;
    int b = 0;
    volatile int count = 0;
    
    a  = 1;
    count = 1;
    b = 2;
    count = 2;
    

    In C/C++, the volatile keyword only guarantees that the count variable cannot be reordered against each other, ie. if count == 2, then count = 1 must necessarily precede it. However, there is neither a guarantee that a == 1, nor that b == 2.

    In Java, given the stronger guarantee defined above, then if count == 1, then the assertion a == 1 must be true. Similarly, if count == 2 then assertion that a == 1 && b == 2 must be true. This is what is means by the strict memory guarantee that Java offers that C/C++ does not.

    However this does not mean that C/C++ will not behave the same way Java does. Whether if it does so depends on (1) whether if the compiler performs any code reordering that may be in a surprising order, but legit order, and (2) whether if the underlying machine architecture upholds the same strict memory order, provided that the compiler does not perform any surprising code reordering.

    For instance, compiling code on gcc with -O0 set on all x86 platforms will conform to (and is stricter than) Java's memory model, but other architectures such as the PowerPC, Alpha, Itanium all uphold a weaker memory model which can exhibit surprising program behaviours that a programmer may not expect. Caveat Lector!

    Anyhow, if you are interested in more memory model consistency rules, you might want to watch Intel's explanation of the x86 memory model where the nuances of memory ordering is explained in good detail. Enjoy!

    0 讨论(0)
  • 2020-12-23 18:34

    In C/C++, volatile has no particular semantics that relate to multithreading, so what behavior it will have in that context is platform-specific. C# and Java provide specific multithreading semantics for volatile. So you know what you're getting and can rely on it.

    0 讨论(0)
  • 2020-12-23 18:51

    For C#/Java, "volatile" tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself. The compiler will then avoid any optimisations that may result in problems if the variable changes "outside of its control".

    In C/C++, "volatile" is needed when developing embedded systems or device drivers, where you need to read or write a memory-mapped hardware device. The contents of a particular device register could change at any time, so you need the "volatile" keyword to ensure that such accesses aren't optimised away by the compiler.

    0 讨论(0)
提交回复
热议问题