View mode: basic / threaded / horizontal-split · Log in · Help
May 09, 2004
volatile block
I'm wondering why volatile is a block-level element in D. I have this
horrible nagging suspicion that this is a dumb question, but I don't see the
answer.

It seems to me that there's no way to tell if a specific access to the
volatile memory needs volatile protection, so why not attach it to the
variable?

Mike Swieton
__
We're weirder for being able to pass for normal.
	- The Pet Rock
May 11, 2004
Re: volatile block
There was an argument about this a while ago.  Essentially, the reason 
that volatile is block-level is because, in many cases, a variable is 
only volatile during certain windows.  Often, a variable is protected by 
a lock and is only volatile after you have released the lock for some 
window.  You add "volatile" to the first line that uses the variable 
AFTER you've regained the lock.  Basically, you're telling the compiler 
that it can cache values thereafter...that the variable is ONLY volatile 
in that one condition.  This is advantageous if you then make use of the 
variable a lot (while continuing to hold the lock); the compiler can 
continue to use the cached value, rather than continually reloading it 
from memory.

The downside, of course, is that some variables are always volatile, 
such as atomically modified integers.  These are variables that are 
always volatile; you have to add volatile to every statement that uses them.

Russ

Mike Swieton wrote:
> I'm wondering why volatile is a block-level element in D. I have this
> horrible nagging suspicion that this is a dumb question, but I don't see the
> answer.
> 
> It seems to me that there's no way to tell if a specific access to the
> volatile memory needs volatile protection, so why not attach it to the
> variable?
> 
> Mike Swieton
> __
> We're weirder for being able to pass for normal.
> 	- The Pet Rock
Top | Discussion index | About this forum | D home