bool ThreadPool::stop() { if (Thread::CurrentID() != mDriverThread) { return false; } mTasksAccess.lock(); if (mState != TPS_RUNNING) { // pool is not running mTasksAccess.unlock(); return true; } mState = TPS_STOPPED; mTasksChanged.notifyAll(); mTasksAccess.unlock(); mRestartWorkersCount = numWorkers(); // wait all workers to be done mWorkersAccess.lock(); while (mWorkers.size() > 0) { mWorkersChanged.wait(mWorkersAccess); } mWorkersAccess.unlock(); return true; }
bool ThreadPool::submitOne(TaskExecutor *executor) { if (numWorkers() == 0) return false; // Find next worker in round-robin fashion. size_t id = nextId_; nextId_ = (nextId_ + 1) % workers_.length(); return workers_[id]->submit(executor); }