Example #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		
	}
Example #2
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
}
Example #3
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;
}