void ThreadPool::growPool(int num) { for (int i = 0; i < num; ++i) { PoolThread *thread = new PoolThread(this); thread->dispatch(); _threads.push_back(thread); } _num_active_threads += num; }
ThreadPool::ThreadPool(int initThreads, int maxThreads) { logger = Logger::getLogger("ThreadPool"); this->lowp = -1; this->highp = -1; this->console = false; this->initThreads = initThreads; this->maxThreads = maxThreads; wpool = new TaskPool; wpool->console = console; tpool = new vector<PoolThread*> ; for (int i = 0; i < initThreads; i++) { PoolThread *thread = new PoolThread(); thread->console = console; thread->execute(); tpool->push_back(thread); } poller = new Thread(&ThreadPool::poll, this); }
void ThreadPool::init(int initThreads, int maxThreads,bool console) { this->console = console; this->lowp = -1; this->highp = -1; this->initThreads = initThreads; this->maxThreads = maxThreads; wpool = new TaskPool; wpool->console = console; tpool = new vector<PoolThread*> ; for (int i = 0; i < initThreads; i++) { PoolThread *thread = new PoolThread(); thread->console = console; thread->execute(); tpool->push_back(thread); } poller = new Thread(&ThreadPool::poll, this); }
void ThreadPool::initializeThreads() { if(runFlag)return; wpool = new TaskPool; wpool->console = console; tpool = new vector<PoolThread*>; for (int i = 0; i < initThreads; i++) { PoolThread *thread = new PoolThread(console); thread->execute(); tpool->push_back(thread); } runFlag = true; poller = new Thread(&ThreadPool::poll, this); wpool->start(); pollerStarted = false; complete = false; m_mutex = new ConditionMutex ; }
ThreadPool::ThreadPool(int initThreads, int maxThreads, int lowp, int highp,bool console) { this->console = console; if (lowp > highp) throw "Low Priority should be less than Highest Priority"; logger = Logger::getLogger("ThreadPool"); this->initThreads = initThreads; this->maxThreads = maxThreads; this->lowp = lowp; this->highp = highp; tpool = new vector<PoolThread*> ; wpool = new TaskPool; wpool->console = console; for (int i = 0; i < initThreads; i++) { PoolThread *thread = new PoolThread(); thread->console = console; thread->execute(); tpool->push_back(thread); } poller = new Thread(&ThreadPool::poll, this); prioritypooling = true; }
void* PoolThread::run(void *arg) { PoolThread* ths = (PoolThread*)arg; ths->m_mutex->lock(); bool console = ths->console; bool fl = ths->runFlag; ths->m_mutex->unlock(); while (fl) { ths->mthread->wait(); Task* task = ths->getTask(); if (task) { try { if(!task->isFuture) task->run(); else { FutureTask* ftask = static_cast<FutureTask*>(task); if(ftask!=NULL) { ftask->result = ftask->call(); ftask->taskComplete(); } else { task->run(); } } if(task->cleanUp) { delete task; } } catch(exception& e) { if(console) { ths->logger << e.what() << flush; } if(task->isFuture) { FutureTask* ftask = static_cast<FutureTask*>(task); if(ftask!=NULL) { ftask->taskComplete(); } } } catch(...) { if(console) { ths->logger << "Error Occurred while executing task" << flush; } if(task->isFuture) { FutureTask* ftask = static_cast<FutureTask*>(task); if(ftask!=NULL) { ftask->taskComplete(); } } } ths->release(); } ths->m_mutex->lock(); fl = ths->runFlag; ths->m_mutex->unlock(); } ths->m_mutex->lock(); ths->complete = true; ths->m_mutex->unlock(); return NULL; }