Exemplo n.º 1
0
        private Thread CreatePhilosopher(LockCounter eatCounter, object left, object right)
        {
            var t1 = new Thread(() =>
            {
                while (eatCounter.Get() < MAX_EAT_COUNT)
                {
                    lock (left)
                    {
                        var lockTaken = false;
                        try
                        {
                            Monitor.TryEnter(right, 100, ref lockTaken);
                            if (lockTaken)
                            {
                                Thread.Sleep(100);
                                eatCounter.IncrementAndGet();
                            }
                        }
                        finally
                        {
                            if (lockTaken)
                            {
                                Monitor.Exit(right);
                            }
                        }
                    }

                    Thread.Sleep(100);
                }
            });


            return(t1);
        }
Exemplo n.º 2
0
        public override long ActiveLockCount()
        {
            LockCounter counter = new LockCounter();

            _exclusiveLocks.forEachKeyValue(counter);
            _sharedLocks.forEachKeyValue(counter);
            return(counter.Locks);
        }
Exemplo n.º 3
0
        public void Setup()
        {
            var eatCounter = new LockCounter();
            var fork1      = new object();
            var fork2      = new object();
            var fork3      = new object();
            var fork4      = new object();
            var fork5      = new object();

            t1 = CreatePhilosopher(eatCounter, fork1, fork2);
            t2 = CreatePhilosopher(eatCounter, fork2, fork3);
            t3 = CreatePhilosopher(eatCounter, fork3, fork4);
            t4 = CreatePhilosopher(eatCounter, fork4, fork5);
            t5 = CreatePhilosopher(eatCounter, fork5, fork1);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("syncronization static type");
            LockCounter lc = new LockCounter();
            Monitor.Enter(lc);
            Thread[] threads = new Thread[5];
            for (int i = 0; i < threads.Length; ++i)
            {
                threads[i] = new Thread(lc.UpdateFields);
                threads[i].Start();
            }
            for (int i = 0; i < threads.Length; ++i)
                threads[i].Join();

            Console.WriteLine("Count: {0}\n\n" , lc.Count);
        }
Exemplo n.º 5
0
        private static void DinningPhilosophersTest()
        {
            var eatCounter = new LockCounter();
            var fork1      = new TMVar <bool>(true);
            var fork2      = new TMVar <bool>(true);
            var fork3      = new TMVar <bool>(true);
            var fork4      = new TMVar <bool>(true);
            var fork5      = new TMVar <bool>(true);

            var t1 = StartPhilosopher(eatCounter, fork1, fork2);
            var t2 = StartPhilosopher(eatCounter, fork2, fork3);
            var t3 = StartPhilosopher(eatCounter, fork3, fork4);
            var t4 = StartPhilosopher(eatCounter, fork4, fork5);
            var t5 = StartPhilosopher(eatCounter, fork5, fork1);

            t1.Join();
            t2.Join();
            t3.Join();
            t4.Join();
            t5.Join();
        }
Exemplo n.º 6
0
        private static Thread StartPhilosopher(LockCounter eatCounter, TMVar <bool> left, TMVar <bool> right)
        {
            var t1 = new Thread(() =>
            {
                while (eatCounter.Get() < MAX_EAT_COUNT)
                {
                    STMSystem.Atomic(() =>
                    {
                        if (!left || !right)
                        {
                            STMSystem.Retry();
                        }

                        left.Value  = false;
                        right.Value = false;
                    });

                    Console.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " eating.");
                    Thread.Sleep(100);
                    Console.WriteLine("Eat count: " + eatCounter.IncrementAndGet());


                    STMSystem.Atomic(() =>
                    {
                        left.Value  = true;
                        right.Value = true;
                    });

                    Thread.Sleep(100);
                }
            });

            t1.Start();

            return(t1);
        }