Esempio n. 1
0
        /// <summary>
        /// Returns the object to the pool.
        /// </summary>
        public void Free([NotNull] Pooled <TPooled> pooled)
        {
            Fail.IfArgumentNull(pooled, nameof(pooled));

            lock (this.syncRoot)
            {
                this.items.Push(pooled);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a pool of objects.
        /// </summary>
        public Pool([NotNull] Func <TPooled> constructor, int initialSize = 1, [CanBeNull] Action <TPooled> destructor = null)
        {
            Fail.IfArgumentNull(constructor, nameof(constructor));

            this.Constructor = constructor;
            this.Destructor  = destructor;
            this.items       = new Stack <Pooled <TPooled> >(initialSize);
            for (var i = 0; i < initialSize; i++)
            {
                var pooled = new Pooled <TPooled>(this);
                this.items.Push(pooled);
            }
        }