Exemple #1
0
        public void AlreadyOwned()
        {
            DateTime start, end;
            object   monitor = new object();

            lock (monitor)
            {
                start = DateTime.UtcNow;
                for (int i = 0; i < Iterations; i++)
                {
                    lock (monitor)
                    {
                    }
                }
                end = DateTime.UtcNow;
            }

            TimeSpan nativeTime = end - start;

            SyncLock syncLock = new SyncLock(Timeout.Infinite);

            using (syncLock.Lock())
            {
                start = DateTime.UtcNow;
                for (int i = 0; i < Iterations; i++)
                {
                    using (syncLock.Lock())
                    {
                    }
                }
                end = DateTime.UtcNow;
            }
            TimeSpan syncLockTime = end - start;

            double factor = syncLockTime.TotalMilliseconds / nativeTime.TotalMilliseconds;

            Console.WriteLine("Performance of SyncLock (lock already owned):");
            Console.WriteLine("Native: {0}", nativeTime);
            Console.WriteLine("SyncLock: {0}", syncLockTime);
            Console.WriteLine("Performance penalty factor: {0:0.00}", factor);
            Console.WriteLine();

            Assert.IsTrue(factor < 10, "SyncLock must not be ridiculously slow");
        }
Exemple #2
0
        public void TimeoutThrowsException()
        {
            Thread t = LockSubjectInOtherThread();

            try
            {
                Thread.Sleep(250);
                using (subject.Lock(500))
                {
                }
                Assert.Fail("Should have thrown an exception");
            }
            catch (LockTimeoutException)
            {
                // Expected
            }
            t.Join();
        }