加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 参考大小结构的原子分配

发布时间:2020-12-16 01:46:25 所属栏目:百科 来源:网络整理
导读:快速问题…假设我有一个包含单个引用成员的C#结构.在.NET中,这个结构的赋值是原子的吗?例如: struct ReadOnlyListT{ private ListT list; // other random functionality}; 我知道简单的旧引用赋值是原子的,只是好奇如果将整个事物包装在一个结构中会产生
快速问题…假设我有一个包含单个引用成员的C#结构.在.NET中,这个结构的赋值是原子的吗?例如:

struct ReadOnlyList<T>
{
    private List<T> list;
    // other random functionality
};

我知道简单的旧引用赋值是原子的,只是好奇如果将整个事物包装在一个结构中会产生任何不同……

谢谢

解决方法

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.

因此,使用唯一引用类型成员分配结构的原子性是实现定义的,您不应该依赖它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读