/// <summary> /// Initializes a new instance of the <see cref="ObjectPool{T}"/> class. /// </summary> /// <param name="initialSize">The initial size of the pool.</param> /// <param name="creator">The delegate used to create new object instances.</param> /// <param name="initializer">The delegate used to initialize an object as it is acquired from the pool /// (when Acquire() is called). Can be null.</param> /// <param name="deinitializer">The delegate used to deinitialize an object as it is freed (when Free() /// is called). Can be null.</param> /// <param name="threadSafe">If true, this collection will be thread safe at a slight performance cost. /// Set this value to true if you plan on ever accessing this collection from more than one thread.</param> public ObjectPool(int initialSize, ObjectPoolObjectCreator <T> creator, ObjectPoolObjectHandler <T> initializer, ObjectPoolObjectHandler <T> deinitializer, bool threadSafe) { if (threadSafe) { _threadSync = new object(); } // Store our delegates Creator = creator; Initializer = initializer; Deinitializer = deinitializer; // Create the initial pool and the object instances _poolObjects = new T[initialSize]; for (var i = 0; i < _poolObjects.Length; i++) { _poolObjects[i] = CreateObject(i); } AssertValidPoolIndexForLiveObjects(); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectPool{T}"/> class. /// </summary> /// <param name="creator">The delegate used to create new object instances.</param> /// <param name="initializer">The delegate used to initialize an object as it is acquired from the pool /// (when Acquire() is called). Can be null.</param> /// <param name="deinitializer">The delegate used to deinitialize an object as it is freed (when Free() /// is called). Can be null.</param> /// <param name="threadSafe">If true, this collection will be thread safe at a slight performance cost. /// Set this value to true if you plan on ever accessing this collection from more than one thread.</param> public ObjectPool(ObjectPoolObjectCreator <T> creator, ObjectPoolObjectHandler <T> initializer, ObjectPoolObjectHandler <T> deinitializer, bool threadSafe) : this(16, creator, initializer, deinitializer, threadSafe) { }