Difference volatile keyword C ++ and Java, you know?

volatile

volatile meaning of the word in English is: volatile, unstable meaning.

Java volatile

There volatile keyword in Java, Java is among the volatile role:

  • Make sure the memory Visibility: read and write variables have a global ordering to ensure that the current thread read value is the latest in memory, rather than the value of the cache in the current thread. But the volatile keyword does not guarantee that the relative order of the thread to read and write variables, so the limited application scenarios.

  • Prohibition instruction reordering (happens-before principle): reordering the JVM instruction to improve the efficiency of operation of the program, without affecting the single-threaded under the premise of the execution result of the execution of instructions optimized. Note that single-threaded, multi-threaded instruction re-ordering may affect the results.

Note Java orderliness means:

  • Operation visible to the current value of the modification operation prior to the current thread and write.
  • During instruction optimization, we can not put it in the back of the implementation of the statement of the volatile variable access, nor can back into its volatile variables statement executed on the front.
C++ volatile

A volatile specifier is a hint to a compiler that an object may change its value in ways not specified by the language so that aggressive optimizations must be avoided.

In C ++ volatile three properties:

  • Mutable
  • Not optimized
  • Sequential

When volatile corresponds explicitly request the compiler to optimize prohibited volatile variables, each variable and require assignment need to explicitly copied from register% eax. The volatile keyword will need to use in embedded programming, under certain circumstances, variable registers may vary. So declared volatile data register section is "volatile", it is necessary to prevent the compiler optimization variables, mandatory load register.

The volatile sequential in C ++ means:

  • C / C ++ Volatile variables, operating between non-Volatile variables, compiler is likely to be exchanged in order.
  • Between C / C ++ Volatile variables, the compiler is able to ensure that no exchange order.

C / C ++ Volatile keyword, and it can not be used to build happens-before semantics, thus making multi-thread programming, the use of volatile to be careful, do not fall into the trap of using volatile variables.

But if you need to achieve a similar Java in volatile effect? Explicit memory barriers can be inserted in the code so that the variable force a refresh CPU registers into memory.

for example:

int n = 1;                                                                                         
int main() {
    volatile int x = n;
    asm volatile("" ::: "memory");
    volatile int y = n;
    return 0;
}
Published 118 original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/104802876