System.Threading.SpinLock.Exit C# (CSharp) Method

Exit() private method

private Exit ( bool useMemoryBarrier ) : void
useMemoryBarrier bool
return void
        public void Exit(bool useMemoryBarrier)
        {
            RuntimeHelpers.PrepareConstrainedRegions ();
            try {}
            finally {
                if (isThreadOwnerTrackingEnabled && !IsHeldByCurrentThread)
                    throw new SynchronizationLockException ("Current thread is not the owner of this lock");

                threadWhoTookLock = int.MinValue;
                do {
                    if (useMemoryBarrier)
                        Interlocked.Increment (ref ticket.Value);
                    else
                        ticket.Value++;
                } while (stallTickets != null && stallTickets.TryRemove (ticket.Value));
            }
        }

Same methods

SpinLock::Exit ( ) : void

Usage Example

示例#1
1
        private static void Main(string[] args)
        {
            SpinLock slock = new SpinLock(false);
            long sum1 = 0;
            long sum2 = 0;
            Parallel.For(0, 10000, i => { sum1 += i; });

            Parallel.For(0, 10000, i =>
            {
                bool lockTaken = false;
                try
                {
                    slock.Enter(ref lockTaken);
                    sum2 += i;
                }
                finally
                {
                    if (lockTaken)
                        slock.Exit(false);
                }
            });

            Console.WriteLine("Num1的值为:{0}", sum1);
            Console.WriteLine("Num2的值为:{0}", sum2);
        }
All Usage Examples Of System.Threading.SpinLock::Exit