/// <summary> /// Queues a new workitem. /// </summary> /// <param name="myThreadPoolEntry">The work item, defined by the delegate and params</param> /// <returns>True, if the item was succesfully queued and False if it could be queued. This might be happen if the queue is full.</returns> public bool QueueWorkItem(ThreadPoolEntry myThreadPoolEntry) { if (!_AddingAllowed) { throw new InvalidOperationException("Cannot queue item for this pool."); } if (_Disposed) { throw new InvalidOperationException("Cannot queue item for a disposed pool."); } //System.Diagnostics.Debug.WriteLine("[GraphThreadPool] BusyWorkers: " + _BusyWorkers + " in Queue: " // + _ThreadEntries.Count + " FreeWorkers: " + _FreeWorkers + " CurThreads: " + _ParallelThreadWorkers.Count + " T:" + DateTime.Now.Ticks); if (_WorkerThreads < _MaxNumberOfParallelThreads && _FreeWorkers == 0 && _WorkerThreads < _Workitems) { AddWorkerThread(); //System.Diagnostics.Debug.WriteLine("[GraphThreadPool] Spawn New Thread: BusyWorkers: " + _BusyWorkers + " in Queue: " + _ThreadEntries.Count + " FreeWorkers: " + _FreeWorkers); } // all workers are free and nothing to do, shutdown one worker else if (_FreeWorkers == _WorkerThreads && _WorkerThreads > _MinNumberOfParallelThreads) { lock (_SuspendNextFreeWorkerLock) { _SuspendNextFreeWorker = true; } } lock (_WorkitemsQueue) { try { _WorkitemsQueue.Enqueue(myThreadPoolEntry); Interlocked.Increment(ref _Workitems); } catch { // The queue might be full return(false); } // there is at least 1 worker waiting for items if (_FreeWorkers > 0) { Monitor.Pulse(_WorkitemsQueue); } } return(true); }
/// <summary> /// Queues a new workitem. /// </summary> /// <param name="myThreadPoolEntry">The work item, defined by the delegate and params</param> /// <returns>True, if the item was succesfully queued and False if it could be queued. This might be happen if the queue is full.</returns> public bool QueueWorkItem(ThreadPoolEntry myThreadPoolEntry) { if (!_AddingAllowed) throw new InvalidOperationException("Cannot queue item for this pool."); if (_Disposed) throw new InvalidOperationException("Cannot queue item for a disposed pool."); //System.Diagnostics.Debug.WriteLine("[GraphThreadPool] BusyWorkers: " + _BusyWorkers + " in Queue: " // + _ThreadEntries.Count + " FreeWorkers: " + _FreeWorkers + " CurThreads: " + _ParallelThreadWorkers.Count + " T:" + DateTime.Now.Ticks); if (_WorkerThreads < _MaxNumberOfParallelThreads && _FreeWorkers == 0 && _WorkerThreads < _Workitems) { AddWorkerThread(); //System.Diagnostics.Debug.WriteLine("[GraphThreadPool] Spawn New Thread: BusyWorkers: " + _BusyWorkers + " in Queue: " + _ThreadEntries.Count + " FreeWorkers: " + _FreeWorkers); } // all workers are free and nothing to do, shutdown one worker else if (_FreeWorkers == _WorkerThreads && _WorkerThreads > _MinNumberOfParallelThreads) { lock (_SuspendNextFreeWorkerLock) { _SuspendNextFreeWorker = true; } } lock (_WorkitemsQueue) { try { _WorkitemsQueue.Enqueue(myThreadPoolEntry); Interlocked.Increment(ref _Workitems); } catch { // The queue might be full return false; } // there is at least 1 worker waiting for items if (_FreeWorkers > 0) Monitor.Pulse(_WorkitemsQueue); } return true; }