When should & shouldn't I use this C# utility class to control threads via Interlocked
问题 I'm trying to understand the logic behind how this class was written, and when I should and shouldn't use it. Any insight would be appreciated internal struct SpinLock { private volatile int lockHeld; private readonly static int processorCount; public bool IsHeld { get { return this.lockHeld != 0; } } static SpinLock() { SpinLock.processorCount = Environment.ProcessorCount; } public void Enter() { if (Interlocked.CompareExchange(ref this.lockHeld, 1, 0) != 0) { this.EnterSpin(); } } private