private static void Writer(Object obj) { TestCase2 testCase = (TestCase2)obj; Stopwatch stopwatch = new Stopwatch(); // 记录获取写锁的等待时间 stopwatch.Start(); Interlocked.Add(ref testCase.baselineWriterWaitCount, 1); testCase.readerWriterLock.EnterWriteLock(); Interlocked.Add(ref testCase.baselineWriterWaitCount, -1); stopwatch.Stop(); // 原子操作,更新写者等待总时间 Interlocked.Add(ref testCase.writeWaitTime, stopwatch.ElapsedMilliseconds); stopwatch.Reset(); // 模仿写操作用时 Thread.Sleep(100); // 释放写锁 testCase.readerWriterLock.ExitWriteLock(); // 原子操作,已完成线程计数器+1,如果全部完成唤醒(通知)主线程 Interlocked.Add(ref testCase.finishedWorkerCount, 1); if (testCase.finishedWorkerCount == testCase.totalThreadNum) { testCase.finished.Set(); } }
private static void WriterBaseline(Object obj) { TestCase2 testCase = (TestCase2)obj; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Monitor.Enter(testCase.monitorLockObj); stopwatch.Stop(); // 原子操作,更新写者等待总时间 Interlocked.Add(ref testCase.writeWaitTime, stopwatch.ElapsedMilliseconds); // 模仿写操作用时 Thread.Sleep(100); Monitor.Exit(testCase.monitorLockObj); // 原子操作,已完成线程计数器+1,如果全部完成唤醒(通知)主线程 Interlocked.Add(ref testCase.finishedWorkerCount, 1); if (testCase.finishedWorkerCount == testCase.totalThreadNum) { testCase.finished.Set(); } }