• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

volatile key word

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic