• Post Reply Bookmark Topic Watch Topic
  • New Topic

volatile key word  RSS feed

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a multi-thread program is run on single processor system, is volatile keyword required?
i am under the impression that volatile is required in a multi-processor sytem to ensure that the cpu access the variable using the main memory but not from the local cpu cache.
 
author
Sheriff
Posts: 23503
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, the volatile keyword is for more than just requesting that no caching takes place with registers and the L1/L2 caches. It also (a) preserves ordering. The compiler or JIT compiler will not reorder the instructions (code motion type optimizations). It also (b) guarantee the atomic nature of all variable sets and gets -- meaning setting and getting longs and doubles will be guaranteed to be atomic.

Second, how likely is it for your java code to be running on a single core system. These days, (c) even entry level computers have more than one core. Can you really assume that it will be running on a single core systems. And... (d) I think register caching may be an issue. While I do believe that the hardware caches are flushed during a context switch, I don't think this is true for registers. Instead, I believe registers are merely saved, to be restored back on the context switch back.

Henry
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!