void EnterMyLockSpin() { for (int i = 0; ; i++) { if (i < 3 && smp) { Thread.SpinWait(20); // Wait a few dozen instructions to let another processor release lock. } else { Thread.Sleep(0); // Give up my quantum. } if (Interlocked.CompareExchange(ref myLock, 1, 0) == 0) { return; } } }