• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
Posts: 23589
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.

Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!