Just reading through the MSDN page about new .NET 4.0 feature SpinLock and can not understand idea behind the following statement:
Do not store SpinLock instances in readonly fields.
My feelings that this is somehow related to value-type specifics but not sure how exactly and why. Could anybody bring more light on this point?
The underlying problem is that the C# compiler creates a copy of a
readonlyvalue type field when you call a non-static method on it and executes that method on the copy – because the method could have side effects that change the value of the struct – which is not allowed forreadonlyfields.For further clarification see “Mutating Readonly Structs”.