示例#1
0
        public void IncrementCapped_ThreadSafety()
        {
            // Arrange
            const int Iterations = 100_000;
            const int Max        = 80_000;

            var counter   = new AtomicCounter();
            var numCapped = 0;

            // Act
            Parallel.For(0, Iterations, i =>
            {
                if (!counter.IncrementCapped(Max))
                {
                    Interlocked.Increment(ref numCapped);
                }
            });

            // Assert
            Assert.Equal(Max, counter.Value);
            Assert.Equal(Iterations - Max, numCapped);
        }