void ThreadPool::run() { for (uint8 x=0; x<m_uiCount; x++) { ThreadPoolThread *thread = new ThreadPoolThread(this, false); thread->onCompleteEvent += delegate(this, &ThreadPool::onThreadComplete); m_vThreadList.push_back( thread ); thread->start(); } while (!isStopped()) { doPause(); removedForced(); uint32 runTaskCount = activeThreads(); if (runTaskCount < m_uiCount) startNewTasks(); runTaskCount = activeThreads(); if ((runTaskCount == m_uiCount || m_vTaskList.empty()) && !isStopped()) m_WaitCondition.wait(); } }
void ThreadPool::forceTask(BaseTask *task) { if (!task) return; m_ForcedMutex.writeLock(); ThreadPoolThread *thread = new ThreadPoolThread(new ThreadPoolTaskSource(task), true); thread->onCompleteEvent += delegate(this, &ThreadPool::onThreadComplete); m_vForcedList.push_back( thread ); thread->start(); m_ForcedMutex.writeUnlock(); }
void ThreadPool::start(const std::shared_ptr<Job> &job, int priority) { job->mPriority = priority; if (priority == Guaranteed) { ThreadPoolThread *t = new ThreadPoolThread(job); t->start(mPriority, mThreadStackSize); return; } std::lock_guard<std::mutex> lock(mMutex); if (mJobs.empty()) { mJobs.push_back(job); } else { if (mJobs.at(mJobs.size() - 1)->mPriority >= priority) { mJobs.push_back(job); } else if (mJobs.at(0)->mPriority < priority) { mJobs.push_front(job); } else { mJobs.push_back(job); std::sort(mJobs.begin(), mJobs.end(), jobLessThan); } } mCond.notify_one(); }