1

I have recently getting myself familiarized with volatile keyword and I see that not reading from main memory directly can result in inconsistency and the so-called visibility problem.

I believe the CPU cache is not specific to any thread. So I was wondering if the volatile keyword will be of any use in singlecore processor?

Holger
  • 285,553
  • 42
  • 434
  • 765
Biscuit Coder
  • 477
  • 4
  • 12
  • You were wondering if the CPU cache is useful in a multicore processor? Or you were wondering if you need to use `volatile` on a machine with one cpu and one core (and presumably single-tasking on some kind of specialized OS)? I'm wondering what kind of answer you're expecting. – Elliott Frisch Jul 11 '17 at 16:39
  • 4
    As far as I know, the memory model and language specification do not mention the number of cores as being important to the result of the execution (they are somtimes refered to as examples of what could go wrong, yes). What is mentionned is that consistency only happens when "happen before relationships" between threads exist. If you want (thread)-safe code, read/write code with"happen before" in mind, not "number of cores in my CPU related to the possibilty of shared caches between JVM and OS implemetion of threads on this given architecture", which is probably even harder to reason about!. – GPI Jul 11 '17 at 16:41
  • @ElliottFrisch Sorry for not being clear. I was wondering if using volatile keyword makes any difference in single core processor. – Biscuit Coder Jul 11 '17 at 17:05
  • 1
    @BiscuitCoder That will depend on complex details of the implementation of the JVM and the nature of that specific processor. The whole point of having a language like Java that precisely specifies what `volatile` does and when you need it is so that you don't have to worry about things like that. – David Schwartz Jul 11 '17 at 18:03

2 Answers2

5

Hardware-level concurrency is obviously an important part of the motivation for the spec, but the spec is quite clear that the requirements apply to the system as a whole; so, for example, the JVM's "just-in-time" (JIT) compiler can legitimately optimize the equivalent of

while (this.var) {
    ... code that provably never modifies var ...
}

to the equivalent of

if (this.var) {
    while (true) {
         ... code that provably never modifies var ...
    }
}

if var is not volatile.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

Well, having a single or multi processors(cores) certainly has impact on the ability of the overall program to run parallely effectively, you still can create multiple Threads and they will have their own cache and volatile keyword will still function as it is intended to - The JLS doesn't specify a processor dependent behavior change for the keyword.

Note that the only thing different is being effectively parallel which is not a determinant of the behavior of the keyword.

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • 2
    A CPU cache is a physical hardware thing, so I don't see how you can claim that each Java software thread will have its own. – ruakh Jul 11 '17 at 17:03
  • @ruakh : What is the concern with CPU cache? That is effectively what OS handles. On the level of JVM, it is quite safe to assume that Thread maintains a cached copy of variables and this is where the volatile keyword comes into play. – Manish Kumar Sharma Jul 11 '17 at 17:07
  • @ruakh: almost every CPU has registers, which work just like caches when holding intermediate results and they *are* thread-local, as the task scheduler of the operating system takes care of letting each task run as if it had its own CPU. Further, the JVM’s optimizer works like adding a cache, when eliminating redundant operations assuming a sequential consistency (and ignoring potential concurrent updates in absence of `volatile`). – Holger Jul 11 '17 at 17:10
  • This is exactly where my confusion started. Most of the articles i saw tried to explain using CPU cache which is showcased as each thread has its own CPU cache. I want to understand if CPU cache has anything to do with the threading. If it is same CPU cache for every thread, then volatile will have no effect in single core processer. You can quote that it ensures it writes to Main memory but where exactly is the problem in the first place is what i am not able to understand. – Biscuit Coder Jul 11 '17 at 17:11
  • @BiscuitCoder : What I do in such confusions - I remember that regardless of the underlying OS and hardware, we follow what JLS specifies and what JVM does. JVM is another machine on top of OS. How can you ever talk about underlying hardware directly while also including discussion about a Java keyword. There is no dependence like that. If there was, Java would not be portable. – Manish Kumar Sharma Jul 11 '17 at 17:14
  • 3
    @Biscuit Coder: there are a lot of articles missing the point. See, if you have a loop like `while(! quit) { doSomething(); }`, and `quit` is not declared `volatile`, the JVM’s optimizer might inline the code of `doSomething()` into the loop, detect that it doesn’t modify `quit` and optimize the code to the equivalent of `if(! quit) { while(true) { doSomething(); } }`, not reading the field `quit` anymore. Then, modifying `quit` in another thread has no effect. For encountering this problem, you don’t need multicore nor CPU caches… – Holger Jul 11 '17 at 17:18
  • @pulp_fiction I totally agree with your argument. I am trying my best to have better understanding reg. the volatile keyword in Java. If that is the case, i cant understand why we need Volatile keyword in the first place. I can easily put it in a sentence saying it is required to avoid caching and to read directly from Main memory but i dont know which cache we are talking about. I suppose i am missing something here. – Biscuit Coder Jul 11 '17 at 17:22
  • 2
    @Biscuit Coder: don’t take the word “cache” too literal. By the way, there is no requirement for `volatile` to cause an access to the main memory. All it mandates, is that the JVM does whatever is necessary to establish the specified inter-thread semantics. – Holger Jul 11 '17 at 17:26
  • @Holger Thanks a lot. That makes sense. – Biscuit Coder Jul 11 '17 at 17:28
  • @Holger: The OP specifically asked about CPU caches, and this answer specifically claimed that "you still can create multiple Threads and they will have their own cache". I don't dispute that there are other cache-like things, but I don't think answers should make blatantly incorrect statements without good reason. – ruakh Jul 11 '17 at 20:00
  • @ruakh: I agree that this answer can be misleading and yours is much better (I happened to write a similar example in a comment at the same time). That’s why yours has the upvotes… – Holger Jul 12 '17 at 06:28