public static void TestLocks(Action <TimeSpan, bool, string> logEntry) { ILocker[] lockStyles = { new NoLock(), new InterlockedSpinLock(), new MonitorLock(), new MutexLock(), new SemaphoreSlimLock(), new SemaphoreLock(), new ReaderWriterLockSlimLock() }; const int count = 1000000; foreach (var ls in lockStyles) { using (ILocker theLock = ls) { for (int mode = 0; mode < 2; mode++) { bool exclusive = (mode == 0); var sw = Stopwatch.StartNew(); CountdownEvent startGate = new CountdownEvent(2); Action work = () => { startGate.Signal(); for (int n = 0; n < count; n++) { theLock.EnterLock(exclusive); counter++; // "the work" theLock.ExitLock(); } }; // Block this thread on the results. Task.WaitAll( Task.Run(work), Task.Run(work)); sw.Stop(); logEntry?.Invoke(sw.Elapsed, exclusive, theLock.GetType().Name); } } } }
public void ThreadX() { long res = 0; for (int i = 0; i < 10000; i++) { _locker.Enter(); Interlocked.Increment(ref concurrent); for (int z = 0; z < 1000; z++) { res += res; } if (concurrent > 1) { Console.WriteLine("{0} concurrency issues!", _locker.GetType().Name); } Interlocked.Decrement(ref concurrent); _locker.Exit(); } }