void* CThreadPool::ThreadFunc(void * threadData) { pthread_t tid = pthread_self(); while(1) { pthread_mutex_lock(&m_pthreadMutex); pthread_cond_wait(&m_pthreadCond,&m_pthreadMutex); cout << "tid:" << tid << " run" << endl; //get task vector<CTask*>* taskList = (vector<CTask*>*)threadData; vector<CTask*>::iterator iter = taskList->begin(); while(iter != taskList->end()) { MoveToBusy(tid); break; } CTask* task = *iter; taskList->erase(iter); pthread_mutex_unlock(&m_pthreadMutex); cout << "idel thread number:" << CThreadPool::m_vecIdleThread.size() << endl; cout << "busy thread number:" << CThreadPool::m_vecBusyThread.size() << endl; //cout << "task to be run:" << taskList->size() << endl; task->Run(); //cout << "CThread::thread work" << endl; cout << "tid:" << tid << " idle" << endl; } return (void*)0; }
//------------------------------------------------------------------------ // 쓰레드 실행 // Task를 실행시킨다. //------------------------------------------------------------------------ void CThread::Run() { while (RUN == m_State) { //1. 태스크 처리 EnterTaskSync(); { TaskItor it = m_Tasks.begin(); while (m_Tasks.end() != it) { CTask *pTask = *it; if (CTask::RR_END == pTask->Run()) { // 태스크 제거 it = m_Tasks.erase(it); delete pTask; } else { ++it; } } } LeaveTaskSync(); //2. 메세지 처리 DispatchMessage(); Sleep(1); } // 남았을지도 모를 메세지를 마지막으로 처리한다. DispatchMessage(); }
void CTask::TaskEntry (void *pParam) { CTask *pThis = (CTask *) pParam; assert (pThis != 0); pThis->Run (); pThis->m_State = TaskStateTerminated; CScheduler::Get ()->Yield (); assert (0); }
void CWorkerThread::OnThreadRun() { while (true) { m_thread_notify.Lock(); // put wait in while cause there can be spurious wake up (due to signal/ENITR) while (m_task_list.empty()) { m_thread_notify.Wait(); } CTask *task = m_task_list.front(); m_task_list.pop_front(); m_thread_notify.Unlock(); task->Run(); delete task; } }
/** * 线程回调函数 */ void* CThreadPool::ThreadFunc(void* threadData) { pthread_t tid = pthread_self(); while (1) { pthread_mutex_lock(&m_pthreadMutex); //没有任务时候休眠线程 while (m_vecTaskQue.size() == 0 && !shutdown) { pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex); } if (shutdown) { pthread_mutex_unlock(&m_pthreadMutex); printf("thread %lu will exitx\n",(unsigned long) pthread_self()); pthread_exit(NULL); } // printf("tid %lu run\n", (unsigned long)tid); deque<CTask*>::iterator iter = m_vecTaskQue.begin(); /** * 取出一个任务并处理之 */ CTask* task = *iter; if (iter != m_vecTaskQue.end()) { task = *iter; m_vecTaskQue.erase(iter); } pthread_mutex_unlock(&m_pthreadMutex); task->Run(); /** 执行任务 */ printf("tid:%lu is idle\n", (unsigned long)tid); } return (void*)0; }
/** * 线程回调函数 */ void* CThreadPool::ThreadFunc(void* threadData) { pthread_t tid = pthread_self(); while (1) { pthread_mutex_lock(&m_pthreadMutex); while (m_TaskList.size() == 0 && !shutdown) { pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex); } if (shutdown) { pthread_mutex_unlock(&m_pthreadMutex); printf("thread %lu will exit\n", pthread_self()); pthread_exit(NULL); } printf("tid %lu run\n", tid); vector<CTask*>::iterator iter = m_TaskList.begin(); /** * 取出一个任务并处理之 */ CTask* task = *iter; if (iter != m_TaskList.end()) { task = *iter; m_TaskList.erase(iter); } pthread_mutex_unlock(&m_pthreadMutex); task->Run(); /** 执行任务 */ printf("tid:%lu idle\n", tid); } return (void*)0; }
//------------------------------------------------------------------------ // 쓰레드 실행 // Task를 실행시킨다. //------------------------------------------------------------------------ void CThread::Run() { while (RUN == m_State) { if (m_Tasks.empty()) // break no task break; //1. 태스크 처리 { AutoCSLock cs(m_TaskCS); TaskItor it = m_Tasks.begin(); while (m_Tasks.end() != it) { CTask *pTask = *it; if (CTask::RR_END == pTask->Run()) { // 태스크 제거 it = m_Tasks.erase(it); delete pTask; } else { ++it; } } } //2. 메세지 처리 DispatchMessage(); Sleep(1); } // 남았을지도 모를 메세지를 마지막으로 처리한다. DispatchMessage(); }