.net System.MemberwiseClone和互锁的写入 [英] .net System.MemberwiseClone and interlocked writes

查看:94
本文介绍了.net System.MemberwiseClone和互锁的写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行值类型数组的MemberwiseClone时:

When performing a MemberwiseClone of an array of value types:

 var arr = new double[100];

如果使用在其他线程上的互锁写入修改了这些双打,那么MemberwiseCloned副本是否有被撕裂的双打的风险?我不关心值是否有些陈旧,只是撕裂以及互锁和memberwiseclone之间的相互作用(我想这可以转换为内存blit类型的操作?)

If these doubles are being modified using an Interlocked write on other threads, will the MemberwiseCloned copy be at any risk of having torn doubles in it? I'm not concerned about having slightly stale values, just tearing and the interaction between interlocked and memberwiseclone (which I guess translates to a memory blit type operation?)

推荐答案

是.在32位操作系统上,甚至可以保证存在撕裂的危险.在64位上,它是实现定义的.我不会轻易冒险,因为即使您测试它不会发生,您的测试也只在您特定的.NET版本和您特定的硬件上进行. 您不能确定.

Yes. On 32bit operating systems this is even guaranteed to have the risk of tearing. On 64bit it is implementation defined. I wouldn't lightly risk it because even if you test that it doesn't happen then your test was only on your particular .NET version and on you particular hardware. You can't really make sure.

在64位上,您可以通过实现自己的clone版本(此速度可能不会慢很多)来可靠地防止撕裂.

On 64bit you can reliably prevent tearing by implementing your own version of clone (which is likely to be not much slower).

这篇关于.net System.MemberwiseClone和互锁的写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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