示例#1
0
 public void TestAtomicInt32OperatorMinus()
 {
     for (var j = 0; j < 10; j++)
     {
         var x     = AtomicInt32.From(1000);
         var tasks = new Task[100];
         for (var i = 0; i < 100; i++)
         {
             tasks[i] = Task.Factory.StartNew(() => x.Minus(5));
         }
         Task.WaitAll(tasks);
         Assert.Equal(500, x.Value);
     }
 }
示例#2
0
 public void TestAtomicInt32OperatorIncrement()
 {
     for (var j = 0; j < 10; j++)
     {
         var atomic = AtomicInt32.From(0);
         var tasks  = new Task[100];
         for (var i = 0; i < 100; i++)
         {
             tasks[i] = Task.Factory.StartNew(() => atomic.Increment());
         }
         Task.WaitAll(tasks);
         Assert.Equal(100, atomic.Value);
     }
 }
示例#3
0
        /// <summary>
        /// Acquires an object from the pool. The method is blocked when there is no object available
        /// in the pool. When there is no object, it waits until any object is returned to the pool.
        /// The method is recommended to use when the objects are sufficient.
        /// </summary>
        /// <returns>The pooled object</returns>
        public T Acquire()
        {
            AtomicInt32 acquiredInvalidCounter = AtomicInt32.From(0);

            T    obj;
            var  spin = new SpinWait();
            bool localValidateOnAcquire = validator.ValidateOnAcquire;

            bool acquired = false;

            TryAcquire(-1, out obj);
            do
            {
                spin.SpinOnce();
                if (null == obj)
                {
                    TryAcquire(-1, out obj);
                }
                else
                {
                    if (localValidateOnAcquire && !validator.Validate(obj))
                    {
                        acquiredInvalidCounter.Increment();
                        locker.EnterWriteLock();
                        try
                        {
                            DoInvalidateObject(obj);
                        }
                        finally
                        {
                            obj = null;
                            locker.ExitWriteLock();
                        }

                        if (acquiredInvalidCounter.Value >= AcquiredInvalidLimit)
                        {
                            throw new InvalidOperationException($"Unable to create a valid object after {acquiredInvalidCounter.Value} attempts.");
                        }
                    }
                    else
                    {
                        acquired = true;
                    }
                }
            } while (!acquired);

            return(obj);
        }
示例#4
0
        public void TestStandard05()
        {
            var tasks   = new Task[4];
            var counter = AtomicInt32.From(0);

            for (var n = 0; n < 4; n++)
            {
                tasks[n] = Task.Factory.StartNew(() =>
                {
                    for (var i = 0; i < 100; i++)
                    {
                        TestPost(i);
                        counter.Increment();
                    }
                });
            }

            Task.WaitAll(tasks);
            Assert.Equal(400, counter.Value);
        }