void ThreadPool::setConcurrentJobs(int concurrentJobs) { if (concurrentJobs == mConcurrentJobs) return; if (concurrentJobs > mConcurrentJobs) { std::lock_guard<std::mutex> lock(mMutex); for (int i = mConcurrentJobs; i < concurrentJobs; ++i) { mThreads.push_back(new ThreadPoolThread(this)); mThreads.back()->start(mPriority, mThreadStackSize); } mConcurrentJobs = concurrentJobs; } else { std::unique_lock<std::mutex> lock(mMutex); for (int i = mConcurrentJobs; i > concurrentJobs; --i) { ThreadPoolThread* t = mThreads.back(); mThreads.pop_back(); lock.unlock(); t->stop(); t->join(); lock.lock(); delete t; } mConcurrentJobs = concurrentJobs; } }
ThreadPool::~ThreadPool() { if (sInstance == this) sInstance = 0; std::unique_lock<std::mutex> lock(mMutex); mJobs.clear(); lock.unlock(); for (List<ThreadPoolThread*>::iterator it = mThreads.begin(); it != mThreads.end(); ++it) { ThreadPoolThread* t = *it; t->stop(); t->join(); delete t; } }