c# - How fields of reference type can be nonvolatile? -
here msdn says volatile
:
the volatile keyword indicates field might modified multiple threads executing @ same time. fields declared volatile not subject compiler optimizations assume access single thread. ensures the up-to-date value present in field @ times.
the volatile keyword can applied fields of these types: reference types.
this state implies fields of reference type not volatile default.
think it's ok treat reference-type field field of value type containing address of object. becomes similar int
type. joe albahari gives examples.
but!... unlike usual value types gc moves objects memory when compacts heap , changes references accordingly. hence 'the up-to-date value' must there. , if how concept of volatility apply reference types?
this state implies fields of reference type not volatile default.
sure. no field treated volatile
default because volatile
comes possibility of sizable performance cost.
i think it's ok treat reference-type field field of value type containing address of object. becomes similar int type.
assume can done without problems. what? fields of scalar types such int
or bool
not treated volatile default.
unlike usual value types gc moves objects memory when compacts heap , changes references accordingly. hence 'the up-to-date value' must there. , if how concept of volatility apply reference types?
you confused utility of volatile
. problem meant address not (a) date value is not there (although volatile
semantics guarantee writes value observable reads follow them in abstract timeline¹).
apart that, meant address situation (b) compiler assumes the code generates party modifying value (or value not being modified @ all), means not choose read value field , instead use "cached copy" has @ hand. if value is being modified third party @ same time going result in program using wrong data calculations.
for more information can refer excellent analysis igor ostrovsky, wherein (a) referred "processor optimizations" , (b) "compiler optimizations".
¹ please note not rigorous definition rather crude approximation.
Comments
Post a Comment