int WorkManagerImpl::Dispatch() { while(m_state > 0) { WaitForWork(); //std::cout << "Start to dispatch ... " << std::endl; Worker* worker = NULL; if(m_state > 0) { boost::unique_lock<boost::mutex> locktasks(m_taskmutex); WorkPtr work; BufferPtr task; if(m_works.size() > 0) work = m_works.front(); if(m_tasks.size() > 0) task = m_tasks.front(); if(work) { if(task) { boost::unique_lock<boost::mutex> lockworkers(m_workermutex); if(m_freelist.size() > 0) { worker = m_freelist.front(); m_freelist.pop_front(); } else { int workercount = m_workers.size(); if(workercount < m_maxworkercount || m_maxworkercount <= 0) { Worker* newworker = new Worker(this, m_stacksize); m_workers.push_back(newworker); worker = newworker; } } } if(worker) { worker->SetWorkTask(work, task); m_works.pop_front(); m_tasks.pop_front(); } } } if(worker) worker->GoToWork(); //m_dispatcher.wait(); //usleep(50 * 1000); } return m_state; }