Пример #1
0
        public void MutextBasicSync()
        {
            int entered = 0;

            SlimMutex mutex = new SlimMutex();
            Action waiter = () =>
            {
                using (mutex.Enter())
                {
                    Verify.True(mutex.IsEntered, "The mutex does not report being entered.");

                    Interlocked.Increment(ref entered);

                    Verify.Equal(1, entered);

                    // simulate workload
                    Thread.Sleep(TimeSpan.FromMilliseconds(1));

                    Verify.Equal(1, entered);

                    Interlocked.Decrement(ref entered);

                    Verify.Equal(0, entered);
                }
            };

            var tasks = new Task[Parallelism];
            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = Task.Run(waiter);
                Thread.Sleep(0);
            }

            Task.WaitAll(tasks);
        }
Пример #2
0
        public void MutextTimedSync()
        {
            SlimMutex mutex = new SlimMutex();
            bool firstEntered = false;
            bool secondEntered = false;
            bool success = true;
            object signal = new object();
            Action first = () =>
            {
                using (mutex.Enter())
                {
                    lock (signal)
                    {
                        firstEntered = true;
                        Monitor.PulseAll(signal);

                        while (!secondEntered)
                        {
                            Monitor.Wait(signal);
                        }
                    }
                };
            };
            Action second = () =>
            {
                using (mutex.TryEnter(TimeSpan.FromMilliseconds(10), out success))
                {
                    lock (signal)
                    {
                        secondEntered = true;
                        Monitor.PulseAll(signal);
                    }
                }
            };

            Task[] tasks = new Task[2];
            tasks[0] = Task.Run(first);

            lock (signal)
            {
                while (!firstEntered)
                {
                    Monitor.Wait(signal);
                }
            }

            tasks[1] = Task.Run(second);

            Task.WaitAll(tasks);

            Verify.False(success, "Mutex was double entered.");
            Verify.True(firstEntered, "Mutex wasn't entered the first time.");
            Verify.True(secondEntered, "Mutext wasn't entered the second time");
        }