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 }
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 }