Exemplo n.º 1
0
        /// <summary>
        /// Enlarge pool and return object. Return null if poolSize exceeds _workerMaximum
        /// </summary>
        /// <returns></returns>
        private WorkerItem <T> TryCreateAndEnlargePool()
        {
            lock (_padLock)
            {
                if (_currentWorkers.Count < WorkerMaximum)
                {
                    var newInstance = new WorkerItem <T>(_workerFactory.CreateInstance(_workerIdentifier.ToString()), true);
                    _currentWorkers.Add(newInstance);

                    return(newInstance);
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        private void InitializeWorkers(int workerMinimum, IWorkerFactory <T> workerFactory)
        {
            var spinLock = new SpinLock();

            Parallel.For(0, workerMinimum, i =>
            {
                bool lockHasBeenTaken = false;

                try
                {
                    var workerItem = new WorkerItem <T>(workerFactory.CreateInstance(_workerIdentifier.ToString()), true);
                    spinLock.Enter(ref lockHasBeenTaken);
                    _currentWorkers.Add(workerItem);
                }
                finally
                {
                    if (lockHasBeenTaken)
                    {
                        spinLock.Exit();
                    }
                }
            });
        }