日期:2014-05-18 浏览次数:20855 次
下面的代码示例说明增加和减少整数值的线程安全方式。SafeInstanceCount 始终为 0。但是 UnsafeInstanceCount 不一定为 0,因为在增加和减少计数之间会出现争用条件。此效果在多处理器计算机上尤其明显。 using System; using System.Threading; class Test { static void Main() { Thread thread1 = new Thread(new ThreadStart(ThreadMethod)); Thread thread2 = new Thread(new ThreadStart(ThreadMethod)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); // Have the garbage collector run the finalizer for each // instance of CountClass and wait for it to finish. GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("UnsafeInstanceCount: {0}" + "\nSafeCountInstances: {1}", CountClass.UnsafeInstanceCount.ToString(), CountClass.SafeInstanceCount.ToString()); } static void ThreadMethod() { CountClass cClass; // Create 100,000 instances of CountClass. for(int i = 0; i < 100000; i++) { cClass = new CountClass(); } } } class CountClass { static int unsafeInstanceCount = 0; static int safeInstanceCount = 0; static public int UnsafeInstanceCount { get {return unsafeInstanceCount;} } static public int SafeInstanceCount { get {return safeInstanceCount;} } public CountClass() { unsafeInstanceCount++; Interlocked.Increment(ref safeInstanceCount); } ~CountClass() { unsafeInstanceCount--; Interlocked.Decrement(ref safeInstanceCount); } }