//------------------------------------------------------------------------------------- TPThread* ThreadPool::createThread(int threadWaitSecond, bool threadStartsImmediately) { TPThread* tptd = new TPThread(this, threadWaitSecond); if (threadStartsImmediately) tptd->createThread(); return tptd; }
//------------------------------------------------------------------------------------- TPThread* ThreadPool::createThread(int threadWaitSecond) { TPThread* tptd = new TPThread(this, threadWaitSecond); tptd->createThread(); return tptd; }
//------------------------------------------------------------------------------------- bool ThreadPool::_addTask(TPTask* tptask) { std::list<TPThread*>::iterator itr = freeThreadList_.begin(); TPThread* tptd = (TPThread*)(*itr); freeThreadList_.erase(itr); busyThreadList_.push_back(tptd); --currentFreeThreadCount_; //INFO_MSG("ThreadPool::currFree:%d, currThreadCount:%d, busy:[%d]\n", // currentFreeThreadCount_, currentThreadCount_, busyThreadList_count_); tptd->task(tptask); #if KBE_PLATFORM == PLATFORM_WIN32 if (tptd->sendCondSignal() == 0) { #else if (tptd->sendCondSignal() != 0) { #endif ERROR_MSG("ThreadPool::addTask: pthread_cond_signal error!\n"); return false; } return true; } //------------------------------------------------------------------------------------- bool ThreadPool::addTask(TPTask* tptask) { THREAD_MUTEX_LOCK(threadStateList_mutex_); if(currentFreeThreadCount_ > 0) { bool ret = _addTask(tptask); THREAD_MUTEX_UNLOCK(threadStateList_mutex_); return ret; } bufferTask(tptask); if(isThreadCountMax()) { THREAD_MUTEX_UNLOCK(threadStateList_mutex_); //WARNING_MSG(fmt::format("ThreadPool::addTask: can't createthread, the poolsize is full({}).\n, // maxThreadCount_)); return false; } for(uint32 i=0; i<extraNewAddThreadCount_; ++i) { bool threadStartsImmediately = i > 0; // 设定5分钟未使用则退出的线程 TPThread* tptd = createThread(ThreadPool::timeout, threadStartsImmediately); if(!tptd) { #if KBE_PLATFORM == PLATFORM_WIN32 ERROR_MSG("ThreadPool::addTask: the ThreadPool create thread error! ... \n"); #else ERROR_MSG(fmt::format("ThreadPool::addTask: the ThreadPool create thread error:{0}\n", kbe_strerror())); #endif } // 所有的线程列表 allThreadList_.push_back(tptd); if (threadStartsImmediately) { // 闲置的线程列表 freeThreadList_.push_back(tptd); ++currentFreeThreadCount_; } else { TPTask * pTask = tptd->tryGetTask(); if (pTask) { busyThreadList_.push_back(tptd); tptd->task(pTask); } else { freeThreadList_.push_back(tptd); ++currentFreeThreadCount_; } tptd->createThread(); } ++currentThreadCount_; } INFO_MSG(fmt::format("ThreadPool::addTask: new Thread, currThreadCount: {0}\n", currentThreadCount_)); THREAD_MUTEX_UNLOCK(threadStateList_mutex_); return true; }