#ifndef _EXECUTETHREAD_H #define _EXECUTETHREAD_H #pragma once #include " .EventDefine.h " #include " .Note.h " class __declspec(dllexport) CEvent; class __declspec(dllexport) CExecuteThread ... {public: CExecuteThread(void); ~CExecuteThread(void); void StartExecuteThread(void); void EndExecuteThread(void); void SetEventPF(OBJECT); void BeginRun(CNote*, CNote*, CNote*);protected: void Run(); void AfterRun(void); static UINT ExecuteThreadProc(LPVOID pParam);private: CWinThread* executethread_p; OBJECT eventpf_p; CNote* tasknode_p; CNote* instancenode_p; CNote* threadnode_p; CEvent threadend_e; CEvent threadrun_e;} ; #endif /* _EXECUTETHREAD_H */ #include " StdAfx.h " #include " .ExecuteThread.h " #include " .EventPF.h " CExecuteThread::CExecuteThread( void ) : executethread_p(NULL) , eventpf_p(NULL) , tasknode_p(NULL) , instancenode_p(NULL) , threadnode_p(NULL) ... {} CExecuteThread:: ~ CExecuteThread( void ) ... { EndExecuteThread();} void CExecuteThread::StartExecuteThread( void ) ... { executethread_p = AfxBeginThread(ExecuteThreadProc,this); if (NULL == executethread_p)...{ executethread_p->m_bAutoDelete = true; }} void CExecuteThread::EndExecuteThread( void ) ... { DWORD exitcode; /**//*↓↓↓ add ↓↓↓*/ if (NULL == executethread_p)...{ return; } threadend_e.SetEvent(); GetExitCodeThread(executethread_p->m_hThread, &exitcode); if(STILL_ACTIVE == exitcode)...{ WaitForSingleObject(executethread_p->m_hThread, INFINITE); } /**//*↑↑↑ add ↑↑↑*/} void CExecuteThread::SetEventPF(OBJECT evpf_p) ... { eventpf_p = evpf_p;} void CExecuteThread::BeginRun(CNote * nodetask_p, CNote * nodeobject_p, CNote * nodethread_p) ... { if (NULL != nodetask_p && NULL != nodeobject_p && NULL != nodethread_p)...{ this->tasknode_p = nodetask_p; this->threadnode_p = nodethread_p; this->instancenode_p = nodeobject_p; } /**//*Start the Event*/ this->threadrun_e.SetEvent();} void CExecuteThread::Run( void ) ... { CMyEvent* myEvent_p; CTask* task_p; CListener* listener_p; task_p = (CTask*)tasknode_p->element_p->Get(); listener_p = task_p->GetListener(); myEvent_p = task_p->GetEvent(); listener_p->Action(NULL, myEvent_p->parameter_p);} void CExecuteThread::AfterRun( void ) ... { if (NULL != eventpf_p)...{ ((CEventPF*)eventpf_p)->AfterRunEvent(tasknode_p, instancenode_p, threadnode_p); } tasknode_p = NULL; threadnode_p = NULL; instancenode_p = NULL; /**//*Start the Event*/ /**/////this->threadend_e.SetEvent();} UINT CExecuteThread::ExecuteThreadProc(LPVOID pParam) ... { CExecuteThread* class_p; HANDLE handles[2]; DWORD wretevent; class_p = (CExecuteThread*)pParam; handles[0] = class_p->threadrun_e.m_hObject; handles[1] = class_p->threadend_e.m_hObject; while (1)...{ wretevent = WaitForMultipleObjects(2,handles,FALSE,100); if (wretevent == WAIT_OBJECT_0)...{ /**//* no event */ break; } else if (wretevent == (WAIT_OBJECT_0 + 1))...{ /**//*do event*/ class_p->Run(); class_p->AfterRun(); } } return 0;} POOL #ifndef _THREADPOOL_H #define _THREADPOOL_H #pragma once #include " .CircularLinkList.h " #include " .ExecuteThread.h " #include " .ThreadElement.h " class __declspec(dllexport) CThreadPool ... {public: CThreadPool(void); ~CThreadPool(void); void Initial(US, OBJECT); bool GetIdleThread(CTokenElement**); bool GetIdleThread(CNote**); void IdleThreadMoveToBusyPool(CNote*); void BusyThreadMoveToIdlePool(CNote*); void Free(void);protected: void CreateIdleThread(US);private: US threadnum; CCircularLinkList idlelist; CCircularLinkList busylist; OBJECT eventpf_p; CCriticalSection threadpool_cs;} ; #endif /* _THREADPOOL_H */ #include " StdAfx.h " #include " .ThreadPool.h " CThreadPool::CThreadPool( void ) : threadnum( 0 ) , eventpf_p(NULL) ... {} CThreadPool:: ~ CThreadPool( void ) ... { Free();} void CThreadPool::Initial(US totalthread, OBJECT evpf_p) ... { US i; eventpf_p = evpf_p; threadnum = totalthread; for (i = 0; i < totalthread; i++)...{ CreateIdleThread(totalthread); }} bool CThreadPool::GetIdleThread(CTokenElement ** element_dp) ... { CNote* note_p; bool result = false; threadpool_cs.Lock(); if (idlelist.GetHeader(¬e_p))...{ *element_dp = note_p->element_p; result = true; } threadpool_cs.Unlock(); return result;} bool CThreadPool::GetIdleThread(CNote ** node_dp) ... { threadpool_cs.Lock(); return idlelist.GetHeader(node_dp); threadpool_cs.Unlock();} void CThreadPool::IdleThreadMoveToBusyPool(CNote * idlenode_p) ... { threadpool_cs.Lock(); idlelist.PullOut(idlenode_p); busylist.PushIn(idlenode_p); threadpool_cs.Unlock();} void CThreadPool::BusyThreadMoveToIdlePool(CNote * busynode_p) ... { threadpool_cs.Lock(); busylist.PullOut(busynode_p); idlelist.PushIn(busynode_p); threadpool_cs.Unlock();} void CThreadPool::Free( void ) ... { CNote* note_p; threadpool_cs.Lock(); while (busylist.GetTail(¬e_p))...{ busylist.Remove(note_p); } while (idlelist.GetTail(¬e_p))...{ idlelist.Remove(note_p); } busylist.Free(); idlelist.Free(); threadpool_cs.Unlock();} void CThreadPool::CreateIdleThread(US totalthread) ... { CThreadElement* threadElement_p; CExecuteThread* executeThread_p; executeThread_p = new CExecuteThread(); threadElement_p = new CThreadElement(); executeThread_p->SetEventPF(eventpf_p); executeThread_p->StartExecuteThread(); threadElement_p->Set(executeThread_p); idlelist.AppendTail((CTokenElement*)threadElement_p);}