java - Increased cost of a volatile write over a nonvolatile write -


i've been reading volatile (https://www.ibm.com/developerworks/java/library/j-jtp06197/) , came across bit says volatile write more expensive nonvolatile write.

i can understand there increased cost associated volatile write given volatile way of synchronization want know how how volatile write more expensive nonvolatile write; perhaps have visibility across different thread stacks @ time @ volatile write made?

here's why, according article have indicated:

volatile writes considerably more expensive nonvolatile writes because of memory fencing required guarantee visibility still cheaper lock acquisition.

[...] volatile reads cheap -- cheap nonvolatile reads

and is, of course, true: memory fence operations bound writing , reads execute same way regardless of whether underlying variable volatile or not.

however, volatile in java more volatile vs. nonvolatile memory read. in fact, in essence has nothing distinction: difference in concurrent semantics.

consider notorious example:

volatile boolean runningflag = true;  void run() {   while (runningflag) { work; } } 

if runningflag wasn't volatile, jit compiler rewrite code to

void run() {    if (runningflag) while (true) { work; } } 

the ratio of overhead introduced reading runningflag on each iteration against not reading @ is, needless say, enormous.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -