System.Threading.ClientSpinWait.SpinOnce C# (CSharp) Method

SpinOnce() public method

public SpinOnce ( ) : void
return void
        public void SpinOnce()
        {
            ntime += 1;

            if (isSingleCpu) {
                // On a single-CPU system, spinning does no good
                Thread.Sleep(0);
            } else {
                if (ntime % step == 0)
                    Thread.Sleep(0);
                else
                    // Multi-CPU system might be hyper-threaded, let other thread run
                    Thread.SpinWait (Math.Min (ntime, maxTime) << 1);
            }
        }

Usage Example

Example #1
0
        public void EnterWriteLock()
        {
            ClientSpinWait sw = new ClientSpinWait();

            do
            {
                int state = rwlock;
                if (state < RwWrite)
                {
                    if (ClientInterlocked.CompareExchange(ref rwlock, RwWrite, state) == state)
                    {
                        return;
                    }
                    state = rwlock;
                }
                // We register our interest in taking the Write lock (if upgradeable it's already done)
                while ((state & RwWait) == 0 && ClientInterlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
                {
                    state = rwlock;
                }
                // Before falling to sleep
                while (rwlock > RwWait)
                {
                    sw.SpinOnce();
                }
            } while (true);
        }
All Usage Examples Of System.Threading.ClientSpinWait::SpinOnce