/// <summary> /// Called when a thread is done working. /// </summary> /// <param name="thread">The thread that is done working.</param> internal void _doneWorking(WorkerThread thread) { lock (_lock) { _waitingThreads.Enqueue(thread); } }
/// <summary> /// Called when a thread is done working. /// </summary> /// <param name="thread">The thread that is done working.</param> internal void DoneWorking(WorkerThread thread) { lock (lock_) { waitingThreads_.Enqueue(thread); } }
/// <summary> /// Called when a thread shuts down. /// </summary> /// <param name="thread">The thread that is shutting down.</param> internal void ShutdownThread(WorkerThread thread) { lock (lock_) { threads_.Remove(thread.ID); // Search the waiting threads, remove the given thread can stop early because order // does not matter. int max = waitingThreads_.Count; for (int i = 0; i < max; i++) { var temp = waitingThreads_.Dequeue(); if (temp == thread) break; waitingThreads_.Enqueue(temp); } } }
/// <summary> /// Resizes the thread pool to fit a new thread object and removes extra threads. /// </summary> void ResizePool() { lock (lock_) { // Check that we have at least one waiting thread. if (waitingThreads_.Count == 0) { var temp = new WorkerThread(this, E_); waitingThreads_.Enqueue(temp); threads_.Add(temp.ID, temp); } // Remove extra threads. while (waitingThreads_.Count > (threads_.Count * WAITING_THREAD_TARGET + MIN_THREAD_COUNT)) { var temp = waitingThreads_.Dequeue(); threads_.Remove(temp.ID); temp.Dispose(); } } }