3 ms·
“Newbie” question: if reading a value without having a lock on it can give you a torn read, wouldn't it be possible to crash the CLR by getting a torn read on a
by TazeTSchnitzel 10y ago
“Newbie” question: if reading a value without having a lock on it can give you a torn read, wouldn't it be possible to crash the CLR by getting a torn read on a reference? Or, does it account for this somehow?
- Eyas 10y agoA reference fits perfectly in a word-sized chunk. A pointer/reference is typically 32 or 64-bits wide, since it is simply pointing to a location in memory.
- dsp1234 10y agofrom the ECMA-335 standard (which is not the latest reference, but is easily accessible): "A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size (the size of type native int) is atomic (see §I.12.6.2) when all the write accesses to a location are the same size." Since references are the native word size, the CLR will ensure that there is no tearing. Note that it is possible to produce a torn read variables that are larger than a native-int, even of native types such as decimal[1]. [0] - http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf http://www.ecma-international.org/publications/files/ECMA-ST... section I.12.6.6, page 102 [1] - http://stackoverflow.com/questions/23262513/reproduce-torn-reads-of-decimal-in-c-sharp http://stackoverflow.com/questions/23262513/reproduce-torn-r...