参考原子分配其尺寸结构体 [英] Atomic Assignment of Reference Sized Structs

查看:111
本文介绍了参考原子分配其尺寸结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题......假设我有一个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
};

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.

这篇关于参考原子分配其尺寸结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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