C# 中线程资源访问互斥锁

来源:互联网 发布:用一套好还是用淘宝好 编辑:程序博客网 时间:2024/04/29 09:43

一个加减数值的例子说明问题

using System;using System.Threading;namespace ThreadLockingDemo{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("incorrect counter");            var c = new Counter();            var t1 = new Thread(() => TestCounter(c));            var t2 = new Thread(() => TestCounter(c));            var t3 = new Thread(() => TestCounter(c));            t1.Start();            t2.Start();            t3.Start();            t1.Join();            t2.Join();            t3.Join();            //每次运行的结果都是未知情况            Console.WriteLine("total count: {0}", c.Count);            Console.WriteLine("----------------");            Console.WriteLine("correct counter");            var c1 = new CounterWithLock();            t1 = new Thread(() => TestCounter(c1));            t2 = new Thread(() => TestCounter(c1));            t3 = new Thread(() => TestCounter(c1));            t1.Start();            t2.Start();            t3.Start();            t1.Join();            t2.Join();            t3.Join();            //返回结果确定            Console.WriteLine("total count: {0}", c1.Count);            Console.ReadKey();        }        static void TestCounter(CounterBase c)        {            for(int i = 0; i <100000;i++)            {                c.Increment();                c.Decrement();            }        }        class Counter : CounterBase        {            public int Count { get; private set; }            //没有对count作线程方案保护            public override void Decrement()            {                Count--;            }            public override void Increment()            {                Count++;            }        }        class CounterWithLock : CounterBase        {            private readonly object _syncRoot = new Object();            public int Count { get; private set; }            public override void Decrement()            {                lock(_syncRoot)                {                    Count--;                }            }            public override void Increment()            {                lock (_syncRoot)                {                    Count++;                }            }        }        abstract class CounterBase        {            public abstract void Increment();            public abstract void Decrement();        }    }}

如果是仅是对数值进行互斥保护C#还提供了另一种现成的方案


using System;using System.Threading;namespace AtomicOperation{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("incorrect counter");            var c = new Counter();            var t1 = new Thread(() => TestCounter(c));            var t2 = new Thread(() => TestCounter(c));            var t3 = new Thread(() => TestCounter(c));            t1.Start();            t2.Start();            t3.Start();            //主线程等待三个线程全部结束            t1.Join();            t2.Join();            t3.Join();            Console.WriteLine("total count:{0}", c.Count);            Console.WriteLine("-------------------------");            Console.WriteLine("correct counter");            var c1 = new CounterNoLock();            t1 = new Thread(() => TestCounter(c));            t2 = new Thread(() => TestCounter(c));            t3 = new Thread(() => TestCounter(c));            t1.Start();            t2.Start();            t3.Start();            t1.Join();            t2.Join();            t3.Join();            Console.WriteLine("total count:{0}", c1.Count);            Console.ReadKey();        }        static void TestCounter(CounterBase c)        {            Console.WriteLine("thread id {0}", Thread.CurrentThread.ManagedThreadId);            for (int i =0; i< 10000; i++)            {                c.Increament();                c.Decreament();            }        }        class Counter:CounterBase        {            private int _count;            public int Count { get { return _count; } }            public override void Decreament()            {                _count--;            }            public override void Increament()            {                _count++;            }        }        class CounterNoLock:CounterBase        {            private int _count;            public int Count { get { return _count; } }            public override void Decreament()            {                Interlocked.Increment(ref _count);            }            public override void Increament()            {                Interlocked.Decrement(ref _count);            }        }        abstract class CounterBase        {            public abstract void Increament();            public abstract void Decreament();        }    }}



0 0