Exemplo n.º 1
0
        public void TestCheckingEnterExitOwnership()
        {
            var monitor = new MonitorSimple();

            var finished = 0;

            var th = new Thread(() =>
            {
                monitor.Enter();
                while (finished == 0) { }
            }) { IsBackground = true };

            th.Start();

            WaitAlittle();

            AssertEx.Throws<SynchronizationLockException>(() => monitor.Exit());

            Thread.VolatileWrite(ref finished, 1);
        }
Exemplo n.º 2
0
        public void TestWait()
        {
            var e = new ManualResetEvent(false);
            var m = new MonitorSimple();

            var testValue = 0;

            var th1 = new Thread(() =>
            {
                m.Enter();
                testValue = 1;
                e.WaitOne();

                m.Wait();
                testValue = 2;
                m.Exit();
            }) { Name = "th1" };

            var th2 = new Thread(() =>
            {
                m.Enter();
                testValue = 100;
                m.Exit();
            }) { Name = "th2" };

            th1.Start();

            WaitAlittle();

            th2.Start();

            WaitAlittle();

            Assert.AreEqual(1, testValue);

            e.Set();

            WaitAlittle();

            Assert.AreEqual(100, testValue);

            m.Enter();
            m.PulseAll();
            m.Exit();

            WaitAlittle();

            Assert.AreEqual(2, testValue);
        }