Ejemplo n.º 1
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		
	}
Ejemplo n.º 2
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		
}