Are reads and writes to unaligned fields in .NET definitely atomic?

后端 未结 1 802
渐次进展
渐次进展 2020-12-11 17:27

The C# specification (ECMA-334 and ISO/IEC 23270) has a paragraph about the atomicity of reads and writes:

12.5 Atomicity of variable referenc

相关标签:
1条回答
  • 2020-12-11 18:02

    I think you're right... there are a few situations where if you deliberately go out of your way, the system won't behave as per the language specification. Importantly, ECMA-335 makes this explicit in partition I section 12.6.6:

    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. Atomic writes shall alter no bits other than those written. Unless explicit layout control (see Partition II (Controlling Instance Layout)) is used to alter the default behavior, data elements no larger than the natural word size (the size of a native int) shall be properly aligned. Object references shall be treated as though they are stored in the native word size.

    (Bold emphasis mine; italics are in the spec.)

    0 讨论(0)
提交回复
热议问题