参考原子分配其尺寸结构体原子、尺寸、分配、结构

2023-09-05 01:28:51 作者:无人暖

快速的问题......假设我有一个C#结构,其中包含一个引用成员。此结构的任务原子在.NET?例如:

 结构ReadOnlyList< T>
{
    私人列表< T>清单;
    //其他随机功能
};
 

我知道,普通的旧基准任务是原子,只是好奇,如果包裹整个事情在一个结构有什么差别......

感谢

解决方案   

12.5原子变量引用

     

读取及以下数据类型的读写应是原子:布尔,字符,字节,为sbyte,总之,USHORT,UINT,整型,浮点和引用类型。此外,当枚举类型的具有基础类型在previous列表写还应原子。读取和其他类型,包括长,ULONG,双,和小数,以及用户自定义类型的读写,不必是原子的。除了库函数为此目的设计的,但不保证的原子读 - 修改 - 写,如在增量或减量的情况下

所以,唯一的引用类型成员的结构转让的原子是实现定义的,你不应该依赖于它。

Quick question... Suppose I have a C# struct that contains a single reference member. Are assignments of this struct atomic in .NET? For Example:

struct ReadOnlyList<T>
{
    private List<T> list;
    // other random functionality
};
最近科学家制造出原子序数为112号的新元素.其相对原子质量为227.该元素原子的核外电子数为 .该元素原子的中子数为 . 题目和参考答案 青夏教育精英家教网

I know that plain old reference assignments are atomic, just curious if wrapping the whole thing up in a struct makes any difference...

Thanks

解决方案

12.5 Atomicity of variable references

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. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.

So atomicity of assignment of a structure with the only reference-type member is implementation defined, and you should not rely on it.