问题
When performing a MemberwiseClone of an array of value types:
var arr = new double[100];
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?)
回答1:
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.
On 64bit you can reliably prevent tearing by implementing your own version of clone (which is likely to be not much slower).
来源:https://stackoverflow.com/questions/10998730/net-system-memberwiseclone-and-interlocked-writes