为什么没有 Volatile.Read(ref T location) where T : struct?

Why is there no Volatile.Read(ref T location) where T : struct?

有一个 Volatile.Read method for all primitives and reference types, why is there no Volatile.Read for structures? Same applies to Volatile.Write. Likewise the old Thread.VolatileRead 方法也没有用于结构的方法。

这背后的原因是什么?我可以在 class 中声明易失性结构,为什么我不能用这些方法进行易失性读取?

如果易失性操作也是原子的,则只有它们是原子的,但对于除最简单的结构(例如,原始或引用类型的一个字段,或适合 64 的任何结构)以外的所有结构,情况并非如此 bits/8 字节)。

例如,您对 768 bits/96 字节结构上的此类 Volatile 方法有什么期望?任何大于支持的最大原子操作的任何东西实际上都会导致多次易失性写入,每一次都将立即可见,没有任何保证。

在 .NET 的 Microsoft 实现中,long 和 double Volatile 方法是原子的。即使在 32 位架构上,也会以在此类架构中使用互锁操作为代价。