Exemplo n.º 1
0
//-------------------------------------------------------------------------------------
TPThread* ThreadPool::createThread(int threadWaitSecond, bool threadStartsImmediately)
{
	TPThread* tptd = new TPThread(this, threadWaitSecond);

	if (threadStartsImmediately)
		tptd->createThread();

	return tptd;
}	
Exemplo n.º 2
0
	static void* threadFunc(void* arg)
#endif
	{
		TPThread * tptd = static_cast<TPThread*>(arg);
		bool isRun = true;
#if KBE_PLATFORM == PLATFORM_WIN32
		THREAD_TRY_EXECUTION;
#else			
		pthread_detach(pthread_self());
#endif
		while(isRun)
		{
			isRun = tptd->onWaitCondSignal();
			tptd->state_ = 1;
			TPTask * task = tptd->getTask();
			
			while(task)
			{
				task->process();									// 处理该任务
				TPTask * task1 = tptd->tryGetTask();				// 尝试继续从任务队列里取出一个繁忙的未处理的任务

				if(!task1)
				{
					tptd->onTaskComplete();
					break;
				}
				else
				{
					tptd->deleteFiniTask(task);
					task = task1;
					tptd->setTask(task1);
				}
			}
		}

#if KBE_PLATFORM == PLATFORM_WIN32
		THREAD_HANDLE_CRASH;
		return 0;
#else	
		pthread_exit(NULL);
		return NULL;
#endif		
	}
Exemplo n.º 3
0
void* TPThread::threadFunc(void* arg)
#endif
{
    TPThread * tptd = static_cast<TPThread*>(arg);
    ThreadPool* pThreadPool = tptd->threadPool();

    bool isRun = true;
    tptd->reset_done_tasks();

#if KBE_PLATFORM == PLATFORM_WIN32
#else
    pthread_detach(pthread_self());
#endif

    tptd->onStart();

    while(isRun)
    {
        if(tptd->task() != NULL)
        {
            isRun = true;
        }
        else
        {
            tptd->reset_done_tasks();
            isRun = tptd->onWaitCondSignal();
        }

        if(!isRun || pThreadPool->isDestroyed())
        {
            if(!pThreadPool->hasThread(tptd))
                tptd = NULL;

            goto __THREAD_END__;
        }

        TPTask * task = tptd->task();
        if(task == NULL)
            continue;

        tptd->state_ = THREAD_STATE_BUSY;

        while(task && !tptd->threadPool()->isDestroyed())
        {
            tptd->inc_done_tasks();
            tptd->onProcessTaskStart(task);
            tptd->processTask(task);							// 处理该任务
            tptd->onProcessTaskEnd(task);

            TPTask * task1 = tptd->tryGetTask();				// 尝试继续从任务队列里取出一个繁忙的未处理的任务

            if(!task1)
            {
                tptd->onTaskCompleted();
                break;
            }
            else
            {
                pThreadPool->addFiniTask(task);
                task = task1;
                tptd->task(task1);
            }
        }
    }

__THREAD_END__:
    if(tptd)
    {
        TPTask * task = tptd->task();
        if(task)
        {
            WARNING_MSG(fmt::format("TPThread::threadFunc: task {0:p} not finish, thread.{1:p} will exit.\n",
                                    (void*)task, (void*)tptd));

            delete task;
        }

        tptd->onEnd();
        tptd->state_ = THREAD_STATE_END;
        tptd->reset_done_tasks();
    }

#if KBE_PLATFORM == PLATFORM_WIN32
    return 0;
#else
    pthread_exit(NULL);
    return NULL;
#endif
}
Exemplo n.º 4
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addTask(TPTask* tptask)
{
    THREAD_MUTEX_LOCK(threadStateList_mutex_);
    if(currentFreeThreadCount_ > 0)
    {
        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 is error!\n");
            THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
            return false;
        }

        THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
        return true;
    }

    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++)
    {
        TPThread* tptd = createThread(300);									// 设定5分钟未使用则退出的线程
        if(!tptd)
        {
#if KBE_PLATFORM == PLATFORM_WIN32
            ERROR_MSG("ThreadPool::addTask: the ThreadPool create thread is error! ... \n");
#else
            ERROR_MSG(fmt::format("ThreadPool::addTask: the ThreadPool create thread is error:{0}\n",
                                  kbe_strerror()));
#endif
        }

        allThreadList_.push_back(tptd);										// 所有的线程列表
        freeThreadList_.push_back(tptd);									// 闲置的线程列表
        ++currentThreadCount_;
        ++currentFreeThreadCount_;

    }

    INFO_MSG(fmt::format("ThreadPool::addTask: new Thread, currThreadCount: {0}\n",
                         currentThreadCount_));

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
    return true;
}

//-------------------------------------------------------------------------------------
bool ThreadPool::hasThread(TPThread* pTPThread)
{
    bool ret = true;

    THREAD_MUTEX_LOCK(threadStateList_mutex_);

    std::list<TPThread*>::iterator itr1 = find(allThreadList_.begin(), allThreadList_.end(), pTPThread);
    if(itr1 == allThreadList_.end())
        ret = false;

    THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

    return ret;
}
Exemplo n.º 5
0
//-------------------------------------------------------------------------------------
TPThread* ThreadPool::createThread(int threadWaitSecond)
{
    TPThread* tptd = new TPThread(this, threadWaitSecond);
    tptd->createThread();
    return tptd;
}
Exemplo n.º 6
0
//-------------------------------------------------------------------------------------
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;
}
Exemplo n.º 7
0
//-------------------------------------------------------------------------------------
bool ThreadPool::addTask(TPTask* tptask)
{
	THREAD_MUTEX_LOCK(threadStateList_mutex_);
	if(currentFreeThreadCount_ > 0)
	{
		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_.size());
		
		tptd->setTask(tptask);												// 给线程设置新任务	
		
#if KBE_PLATFORM == PLATFORM_WIN32
		if(tptd->sendCondSignal()== 0){
#else
		if(tptd->sendCondSignal()!= 0){
#endif
			ERROR_MSG("ThreadPool::addTask: pthread_cond_signal is error!\n");
			THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
			return false;
		}
		
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
		return true;
	}
	
	bufferTask(tptask);
	
	if(isThreadCountMax())
	{
		THREAD_MUTEX_UNLOCK(threadStateList_mutex_);

		WARNING_MSG(boost::format("ThreadPool::addTask: thread create is failed, count is full(%1%).\n") % 
			maxThreadCount_);

		return false;
	}

	for(uint32 i=0; i<extraNewAddThreadCount_; i++)
	{
		TPThread* tptd = createThread(300);									// 设定5分钟未使用则退出的线程
		if(!tptd)
		{
#if KBE_PLATFORM == PLATFORM_WIN32		
			ERROR_MSG("ThreadPool::addTask: ThreadPool create new Thread error! ... \n");
#else
			ERROR_MSG(boost::format("boost::format(ThreadPool::addTask: ThreadPool create new Thread error:%1% ... \n") % 
				kbe_strerror());
#endif				
		}

		allThreadList_.push_back(tptd);										// 所有的线程列表
		freeThreadList_.push_back(tptd);									// 闲置的线程列表
		currentThreadCount_++;
		currentFreeThreadCount_++;	
		
	}
	
	INFO_MSG(boost::format("ThreadPool::addTask: new Thread, currThreadCount: %1%\n") % 
		currentThreadCount_);

	THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
	return true;
}

//-------------------------------------------------------------------------------------
#if KBE_PLATFORM == PLATFORM_WIN32
unsigned __stdcall TPThread::threadFunc(void *arg)
#else	
void* TPThread::threadFunc(void* arg)
#endif
{
	TPThread * tptd = static_cast<TPThread*>(arg);
	bool isRun = true;

#if KBE_PLATFORM == PLATFORM_WIN32
#else			
	pthread_detach(pthread_self());
#endif

	tptd->onStart();

	while(isRun)
	{
		if(tptd->getTask() != NULL)
		{
			isRun = true;
		}
		else
		{
			isRun = tptd->onWaitCondSignal();
		}

		if(!isRun || tptd->threadPool()->isDestroyed())
		{
			goto __THREAD_END__;
		}

		TPTask * task = tptd->getTask();
		if(task == NULL)
			continue;

		tptd->state_ = THREAD_STATE_BUSY;
		while(task && !tptd->threadPool()->isDestroyed())
		{
			tptd->onProcessTaskStart(task);
			tptd->processTask(task);							// 处理该任务								
			tptd->onProcessTaskEnd(task);

			TPTask * task1 = tptd->tryGetTask();				// 尝试继续从任务队列里取出一个繁忙的未处理的任务

			if(!task1)
			{
				tptd->onTaskComplete();
				break;
			}
			else
			{
				tptd->deleteFiniTask(task);
				task = task1;
				tptd->setTask(task1);
			}
		}
	}

__THREAD_END__:
	TPTask * task = tptd->getTask();
	if(task)
	{
		WARNING_MSG(boost::format("TPThread::threadFunc: task %1% not finish, thread.%2% will exit.\n") % 
			task % tptd);

		delete task;
	}

	if(tptd)
		tptd->onEnd();
	
	tptd->state_ = THREAD_STATE_END;

#if KBE_PLATFORM == PLATFORM_WIN32
	return 0;
#else	
	pthread_exit(NULL);
	return NULL;
#endif		
}