Introduction Multithreading is a good program mechanism, using threads to fully utilize computer resources, perform parallel business processing. But good things always have its shortcomings. The thread is good, but abuse, or improper management will cause thread chaos, memory vulnerabilities, resulting in slowing the computer speed, slow response. The idea then files can handle threads yourself to handle this complex work to your computer. After years of practice, the author finally implemented this purpose with VC . Everyone knows in VC we can create a thread with CreateThread, which returns the Handle of this thread, and we can use this handle to manage this thread. So we can do a class named ThreadManager to manage these Handle and monitor the status of these threads at any time. The total thinking is this, but in order to facilitate usage, we will optimize this class into a DLL form, so we can use this class in any program to manage threads (of course if you are interested, You can use a COM model to be implemented). Implementation Because we have to give all the work to ThreadManager, the thread creation, monitoring, and deleting will be implemented in this class, and we only have to pass the thread function name and thread parameters to the ThreadManager class, so we define it The class constructor is: ThreadManager (ThreadPrc ThreadPro, LPVOID PPARM); this class also can start threads, so we define a startup function: RuntHread (); : HANDLE GetThreadHandle (); while the parameter is the only member of the class Handle: HANDLE m_Handle; ThreadManager then defined as follows: typedef DWORD (WINAPI * ThreadPro) (LPVOID); class CThreadManager: public CObject {DECLARE_DYNAMIC (CThreadManager) public: CThreadManager (ThreadPrc Threadpro, lpvoid pParam; ~ cthreadManager (); CBOOL RUNTHREAD (); handle getthreadhandle () const {return (m_handle);
Private: Handle M_Handle;}; . This class is defined as follows:
class CThreadTask: public CObject {DECLARE_DYNAMIC (CThreadTask) public: CThreadTask (); Constructor ~ CThreadTask (); CBOOL IsValid (); void AddHandle (CONST HANDLE cHandle); add thread handle to m_ObList the void CloseThreadHandles (); (off thread ) Static cthreadtask & getcthreadtask (); pointer for getting ThreadTask classes in the ManageRThread class
Coblist m_oblist; thread handle group handle m_handle; thread handle BOOL M_BKEEPGOING; is running};
Implement_dynamic (CTHReadcaretaker, CObject) where isvalid () is used to test whether ThreadTaskFunc () has been dynamped, and other means are obvious. The flowchart of the flow chart is as follows: The specific implementation is given below. ThreadTask :: ThreadTask () {m_bKeepGoing = TRUE; indicates operation DWORD nThreadId = 0; m_Handle = (HANDLE) :: CreateThread (NULL, 0, ThreadTaskpro, 0,0, & nThreadId); Create a thread management} void ThreadTask :: AddHandle (CONST HANDLE cHandle) {CHandle * pHandle = new CHandle; pHandle-> m_ThreadHandle = cHandle; m_ObList.AddTail (pHandle);} void ThreadTask :: CloseThreadHandles () {if (m_ObList.GetCount ()) {POSITION pos1, pos2; Chandle * Phandle = (Chandle *) NULL; (Chandle class is quite simple, only one member function m_threadhandle) dword dwexitcode = 0L; for (POS1 = m_oblist.getHeadPosition (); (POS2 = POS1)! = Position (null);) {All existing thread handles PHANDLE = Dynamic_DOWNCAST (Chandle, M_oblist.getNext (POS1)); get POS1 Handle Verify (:: getExitCodetHread (Phandle-> m_threadhandle, & dwexitcode); get the current status of thread Phandle IF (DwExitcode! = STILL_ACTIVE) If the {released current handle m_oblist.removeat (POS2); verify (:: closehandle (phandle-> m_threadhandle); delete phandle; phandle = (chandle *) null;}}}}
Threadtask :: ~ threadtask () {
}
ThreadTask & ThreadTask :: GetThreadTask () {return ThreadTask static object to ThreadManager class calls static ThreadTask Taker; return (Taker);} BOOL ThreadTask :: IsValid () {if already running BOOL bValid_Status = FALSE; if ((this =! NULL) && AfxIsValidAddress (this, sizeof (ThreadTask))) bValid_Status = TRUE; return (bValid_Status);} Here thread ThreadTaskpro (); the thread is the main purpose of the call ThreadTask CloseThreadHandles () function, implemented as follows: DWORD WINAPI ThreadTaskpro (LPVOID pParam) {hANDLE hCurrentThread = GetCurrentThread (); obtaining the thread handle SetThreadPriority (hCurrentThread, THREAD_PRIORITY_LOWEST); set minimum while (ThreadTask :: GetThreadTask () m_bKeepGoing.) {Sleep (500); rest 500mms ThreadTask :: GetThreadTask ();} Setthreadpriority (hcurrentthread, thread_priority_normal); return (0);} ThreadTask class is over, let's take a look at the implementation of the ThreadManager class, in fact, its work is small, it is to manage Thread (completed in the constructor) and control the ThreadTask class. Specific implementation: ThreadManager :: ThreadManager (ThreadPro Threadpro, lpvoid pParam) {dword nthreadID = 0;
_Asserte (threadtask :: getthreadtask (). Isvalid ()); Run ThreadTask class m_handle = (Handle) :: CreateThread (Null, 0, ThreadPro, (LPVOID) PParam, 0, & nthreadID; create thread to be managed, and return handle if (ThreadTask :: GetThreadTask () m_bKeepGoing.) {If running then ThreadTask ASSERT (m_Handle); ThreadTask :: GetThreadTask () AddHandle (m_Handle);. the handler class ThreadTask added in order to manage}} ThreadManager :: ~ ThreadManager () {
} BOOL ThreadManager :: Runthread () {Running Return (: ResumeTHRead (M_Handle)! = 0xfffffffff);} The whole mechanism is over. How to use the method is quite simple, for example, I want to run the process mythread1, and pass the parameter m_pro, just use the following code: ThreadManager threadManager (& mythread1, (lpvoid) m_pro); threadManager.Runthread (); The thread created with this method will automatically manages ThreadManager class, and automatically release thread resources when completed. Zhao Ming Email: papaya_zm@sina.com; zmpapaya@hotmail.comweb: http://h2osky.126.com