static unsigned long WINAPI thread_proc(void* param) { Thread * t = (Thread*)param; t->SetupMutex.Acquire(); uint32 tid = t->ControlInterface.GetId(); bool ht = (t->ExecutionTarget != NULL); t->SetupMutex.Release(); for(;;) { if(t->ExecutionTarget != NULL) { if(t->ExecutionTarget->run())//执行任务,返回true表示任务完成 delete t->ExecutionTarget; t->ExecutionTarget = NULL; } if(!ThreadPool.ThreadExit(t)) { //Log.Debug("ThreadPool", "Thread %u exiting.", tid); break; } else { //if(ht) // printf("ThreadPool:线程%d正在等待新任务.", tid); t->ControlInterface.Suspend();//暂停线程运行 } } ExitThread(0); return 0; }
static unsigned long WINAPI thread_proc(void* param) { Thread * t = (Thread*)param; t->SetupMutex.Acquire(); uint32 tid = t->ControlInterface.GetId(); bool ht = (t->ExecutionTarget != NULL); t->SetupMutex.Release(); Log.Debug("ThreadPool", "Thread %u started.", t->ControlInterface.GetId()); for(;;) { if(t->ExecutionTarget != NULL) { if(t->ExecutionTarget->run()) delete t->ExecutionTarget; t->ExecutionTarget = NULL; } if(!ThreadPool.ThreadExit(t)) { Log.Debug("ThreadPool", "Thread %u exiting.", tid); break; } else { if(ht) Log.Debug("ThreadPool", "Thread %u waiting for a new task.", tid); // enter "suspended" state. when we return, the threadpool will either tell us to fuk off, or to execute a new task. t->ControlInterface.Suspend(); // after resuming, this is where we will end up. start the loop again, check for tasks, then go back to the threadpool. } } // at this point the t pointer has already been freed, so we can just cleanly exit. ExitThread(0); }