void WorkerManager::OnFinishTask(WorkerThread* worker) { AutoLocker lock(m_critSec); assert(CountWorker(m_activeWorkers, worker) == 1 && "activeWorkers must contain the worker"); workers_type::iterator it = FindWorker(m_activeWorkers, worker); if(!ExceedsMaxWorkers()) { RegisterInactiveWorker(*it); } m_activeWorkers.erase(it); }
void WorkerPool::dispatch() { //lock //if(get job) //this is a worker ,sign has work //else //set empty //AutoLock ato_lck(lock_); //线程主函数 HANDLE realThread = NULL; unsigned long id = ::GetCurrentThreadId(); WorkerImpl* impl = FindWorker(id); assert(NULL != impl); while (!exit_thread_) { //AutoLock lock(lock_); ParamInternal* inner; if (getJob(inner)) { //first unlock //激活等待的线程 NotifyIdle(); inner->param->Run(); } else { printf("Thread %s Wait\n",impl->GetName()); //等待有任务或者获取任务的信号 idle_evnet_.Wait(); } } impl->Stop(); }