Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpinLock and readonly fields

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?

like image 533
sll Avatar asked Feb 10 '12 21:02

sll


1 Answers

The underlying problem is that the C# compiler creates a copy of a readonly value 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 for readonly fields.

For further clarification see "Mutating Readonly Structs".

like image 168
BrokenGlass Avatar answered Oct 30 '22 09:10

BrokenGlass