SuspendThread question
-
Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }
-
Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }
Why do you call suspendthread? If you suspend the thread it will not exit and thus if you wait on the thread handle it will not ever be signaled. It's only signaled when the thread exits and unless you have another thread calling "ResumeTHread" that thread will just sit around. Also, I know you didn't do this up there but never suspend a thread from another thread; it could be holding a lock and thus deadlock the process since you don't know what it's doing when you suspend it. 8bc7c0ec02c0e404c0cc0680f7018827ebee
-
Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }
See here for an interesting article about why you shouldn't (in general) call
SuspendThread
. If you want one thead to wait until another is finished try code like this:WaitForSingleObject(m_hThreadF1, INFINITE);
A thread handle is a waitable object - It becomes signalled when the thread exist. Steve
-
Hi all - Here is the problem. I need to create five worker threads in my win 32 dll and keep them alive throughout the host process life time. I need to call certain routines in these threads as per the host process's calls. I want to know what are the possible loopholes in this code. Is it correct or totally wrong? Your expert and experienced comments will be greatly appreciated.. The code looks like HANDLE m_hThreadF1 = 0; //global thread handles HANDLE m_hThreadF2 = 0; HANDLE m_hThreadF3 = 0; DLL::Function1() //this routine gets called by the host process { if (m_hThreadF1!=0) ResumeThread(m_hThreadF1) else m_hThreadF1 = CreateThread(,,ThreadFuncF1,0); //not in suspended mode //Wait on the above thread to finish, then return, probably via an event } ThreadFuncF1() //thread handler { //do the intended processing SuspendThread(m_hThreadF1) }
some tips, uh? the method, WaitforSingleObject(threadhandle, waittime) will return a value equal to WAIT_TIMEOUT if the thread is still alive (not in signalled state). instead of if(m_hThreadF1 != 0) try using this method with waittime as zero, as below: if(WaitForSingleObject(m_hThread, 0)==WAIT_TIMEOUT) // The thread s alive else // The thread s signalled, spawn new make sure that the pThread's m_autodelete s set to false. u mentioned, the thread s supposed to be alive till da end. in this case, it seems, ur thread func has to make a slight modification. ResumeThread(handle), will resume a thread from the point it has been suspended, so, in ur case, when the ThreadFuncF1 resumes, it jst exits!!! ThreadFuncF1() { do{ // do the intended processing // Finished Processing, Notify parent SuspendThread(m_hThreadF1) }while(TRUE); // do processing again } hope thes'll help. :sigh: thanks, -- 'whiz'