2

I know how volatile works but question about how especially. Every often I hear that persons which сonsider themselves experts multithreading like debate about how volatile works.

Usually for novice persons explain volatile like: every thread has own cach where different variables can store but volatile don't cached. "experts" began try to be clever and said that it is absolutely wrong - volatile cashed in processor cash but volatile don't cash in processor register.

I cannot find information about it.

Can you confirm or decline this opinion?

P.S. I know that java programmer should know JMM for resolve volatile related questions but I want to get exact answer.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • I _guess_ it goes like this: `volatile` may be very well in register. But special additional assembly instruction, which tell other processors to invalidate their caches, are emitted every time `volatile` is written from register to memory and another assembly op, that tells reading processor to invalidate _its_ cache, emitted when reading `volatile` from memory to register. – Victor Sorokin Jul 04 '14 at 18:01
  • http://stackoverflow.com/questions/6259745/volatile-variable-in-java – Amir Afghani Jul 04 '14 at 18:02
  • Who is downvoter? answer of Amit G shows that this opinion really exist among of programmer. – gstackoverflow Jul 04 '14 at 20:35

2 Answers2

7

Take a look at the following example. It contains read and write operations to a volatile variable. However, most likely, the JIT compiler will generate code, in which the variable will be kept in a CPU register the whole time.

final class Int {
    public volatile int value;
    public Int(int value) { this.value = value; }
}

int sum(int n) {
    int result = 0;
    for (int i = 0; i < n; ++i) {
        result += new Int(i).value;
    }
    return result;
}

The point is: The Java Language Specification doesn't talk about CPU registers or CPU caches. It specifies the semantics based on an abstract machine. It's up to the Java Runtime Environment to execute the code on a concrete machine in such a way, that it fulfils the specification. If the JIT compiler can detect, that it is okay to keep a volatile variable in a CPU register, then there is no need to write it to memory.

nosid
  • 48,932
  • 13
  • 112
  • 139
0

From http://www.javaperformancetuning.com/news/newtips118.shtml

volatile variables cannot be allocated in CPU registers, which makes them less efficient than normal variables.

This can not be done for register, since they are not shared between different threads executing on processor. So changes made by one thread will never be visible to other.

Caches are memory backed and can be shared between processors. Volatile variable can be cached, depending on memory consistency model of architecture and implementation of JVM on it.

Amit G
  • 2,293
  • 3
  • 24
  • 44
  • **Note: volatile variable can be cached, depends on memory consistency model of architecture.** I don't understand context. cфn be cached WHERE? – gstackoverflow Jul 04 '14 at 18:15
  • cached in L1/L2 cache memory. small fast memory on processor, based on concept of locality. works at about 10x RAM. – Amit G Jul 04 '14 at 18:21