在读取和写入未对齐的字段,.NET肯定原子? [英] Are reads and writes to unaligned fields in .NET definitely atomic?

查看:187
本文介绍了在读取和写入未对齐的字段,.NET肯定原子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的规范( ECMA-334 和<一href="http://standards.iso.org/ittf/PubliclyAvailableStandards/c042926_ISO_IEC_23270_2006%28E%29.zip">ISO/IEC 23270 )具有对原子一个段落的读取和写入:

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

12.5原子变量引用

读取及以下数据类型的读写应是原子:布尔,字符,字节,为sbyte,总之,USHORT,UINT,整型,浮点和引用类型。此外,当枚举类型的具有基础类型在previous列表写还应原子。读取和其他类型,包括长,ULONG,双,和小数,以及用户自定义类型的读写,不必是原子的。

Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list shall also be atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, need not be atomic.

不过,我也很难想象这是总是正确的。例如,我可以用一个布局结构中的<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx"><$c$c>StructLayout属性,并强制字段没有对齐:

But I have a hard time imagining that to be always true. For example, I can layout a struct using the StructLayout attribute, and force the fields to be unaligned:

// sizeof(MyStruct) == 9
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct MyStruct
{
    public byte pad;   // Offset: 0
    public int value1; // Offset: 1
    public int value2; // Offset: 5
}

现在,当我做到这一点,我会觉得在写 INT 没有的原子,因为它没有对准自然边界

Now when I do this, I would think the write to the int is not atomic, since it is not aligned to the natural boundary:

MyStruct myStruct = new MyStruct();
myStruct.value1 = 20;

那么,是不是绝对原子(如规范说)呢,还是不能保证是原子的(例如在x86)?无论哪种方式,你有什么资源来支持这一行动?

So, is it definitely atomic (like the specification says), or is it not guaranteed to be atomic (e.g. on x86)? Either way, do you have any sources to back this up?

推荐答案

我觉得你说得对......有一些情况下,如果你刻意去追求自己的方式,系统将不会表现为每语言规范。重要的是, ECMA-335 使得这个明确的分区我第12.6.6:

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:

一个符合CLI须保证读取和写入访问的对齐内存的   位置并不比本地字大小(原始类型为int的大小)放大是原子   (见§I.12.6.2)当所有的写访问的位置是相同的大小。原子写入应   改变没有比特比写其他。 除非显式布局控制(见   二分区(控制实例布局))来改变默认行为,,数据元素没有   比天然字大小(天然int的长度)较大须正确对齐。目的   引用应被处理,就好像它们被存储在本地字大小。

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.)

这篇关于在读取和写入未对齐的字段,.NET肯定原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆