예제 #1
0
        public void int_array_is_correct_with_concurrency(int total, int threadCount)
        {
            var array  = new AtomicIntArray(100);
            var thread = new List <Thread>();

            for (var i = 0; i < threadCount; i++)
            {
                thread.Add(new Thread(() =>
                {
                    int index = 0;
                    for (long j = 0; j < total; j++)
                    {
                        array.Increment(index++);
                        if (index == array.Length)
                        {
                            index = 0;
                        }
                    }
                }));
            }

            thread.ForEach(t => t.Start());
            thread.ForEach(t => t.Join());

            long sum = 0;

            for (int i = 0; i < array.Length; i++)
            {
                sum += array.GetValue(i);
            }

            sum.Should().Be(total * threadCount);
        }
        public void Can_decrement_by_value()
        {
            var array = new AtomicIntArray(1);

            array.Increment(0, 10);
            array.Decrement(0, 2);

            array.GetValue(0).Should().Be(8);
        }
        public void Can_increment_value()
        {
            var list  = new[] { 1, 2, 3 };
            var array = new AtomicIntArray(list);

            array.Increment(0);

            array.GetValue(0).Should().Be(2);
        }
        public void Can_get_and_reset()
        {
            var array = new AtomicIntArray(1);

            array.Increment(0);

            var value = array.GetAndReset(0);

            value.Should().Be(1);
            array.Length.Should().Be(1);
            array.GetValue(0).Should().Be(0);
        }