public void D_Unsafe_InstanceImplementation()
        {
            var results = new ConcurrentBag <int>();
            var opt     = new ParallelOptions {
                MaxDegreeOfParallelism = Iterations
            };

            ITwoPlusTwo implementation = new InstanceTwoPlusTwo();

            Parallel.For(0, Iterations, opt, i =>
            {
                var result = implementation.CalculateTwoPlusTwo();
                results.Add(result);
            });

            Assert.IsEmpty(results.Where(r => r != RightAnswer));
        }
        public void F_Safe_InstanceImplementation_ThreadLocalInstances()
        {
            // Here we guarantee thread safety by giving each thread its own instance, but this is not always possible or desirable
            var results = new ConcurrentBag <int>();
            var opt     = new ParallelOptions {
                MaxDegreeOfParallelism = Iterations
            };

            Parallel.For(0, Iterations, opt, i =>
            {
                // The following line was moved inside the parallel loop so that each thread has its own instance
                ITwoPlusTwo implementation = new InstanceTwoPlusTwo();
                var result = implementation.CalculateTwoPlusTwo();
                results.Add(result);
            });

            Assert.IsEmpty(results.Where(r => r != RightAnswer));
        }
        public void A_SanityCheck()
        {
            ITwoPlusTwo implementation = new StaticTwoPlusTwo();
            var         result         = implementation.CalculateTwoPlusTwo();

            Assert.AreEqual(RightAnswer, result);

            implementation = SingletonTwoPlusTwo.Instance;
            result         = implementation.CalculateTwoPlusTwo();
            Assert.AreEqual(RightAnswer, result);

            implementation = new InstanceTwoPlusTwo();
            result         = implementation.CalculateTwoPlusTwo();
            Assert.AreEqual(RightAnswer, result);

            implementation = new ThreadSafeInstanceTwoPlusTwo();
            result         = implementation.CalculateTwoPlusTwo();
            Assert.AreEqual(RightAnswer, result);

            implementation = new LocalTwoPlusTwo();
            result         = implementation.CalculateTwoPlusTwo();
            Assert.AreEqual(RightAnswer, result);
        }