Пример #1
0
        private static void RunIncrementAccumulatorSafe()
        {
            var a       = new ExperimentsWithInterlocked();
            var thread1 = new Thread(a.IncrementAccumulatorSafe);
            var thread2 = new Thread(a.IncrementAccumulatorSafe);

            thread2.Start();
            thread1.Start();
            thread1.Join();
            Console.WriteLine("   " + a.Accum);
            thread2.Join();
            Console.WriteLine("Result " + a.Accum + " " + (a.TrueResult == a.Accum));
        }
Пример #2
0
        private static void RunIncrementSafe()
        {
            var a       = new ExperimentsWithInterlocked();
            var thread1 = new Thread(a.IncrementUnsafe);
            var thread2 = new Thread(a.IncrementUnsafe);
            var thread3 = new Thread(a.IncrementUnsafe);
            var thread4 = new Thread(a.IncrementUnsafe);

            thread3.Start();
            thread4.Start();
            thread2.Start();
            thread1.Start();
            thread1.Join();
            Console.WriteLine("   " + a.Accum2);
            thread2.Join();
            thread3.Join();
            thread4.Join();
            Console.WriteLine("Result " + a.Accum2);
        }