Re: WaitForSingleObject sleeps forewer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



agentd <agentd@xxxxxxxxxxxxxxxx> wrote:

Hi thanks for the information but still no solution

I see it! I see it! Is there a prize?

inline uintmsd_t Thread::_ThreadExecute(void* Ptr)
{
if(NULL==Ptr)
{
assert(NULL==Ptr);
return 1L;
}
reinterpret_cast<Thread*>(Ptr)->Execute();
return 0L;
}

This is not related to your problem, but are you aware that this "assert"
will never fire? I suspect you wanted to have the "assert" BEFORE the
"if".

inline bool Thread::Start()
{
if(NULL==m_ThreadHandle)
{
m_ThreadHandle=reinterpret_cast<HANDLE>(_beginthreadex(NULL,0,Thread::_ThreadExecute,this,0,NULL));
BOOL r =
::SetThreadPriority((HANDLE)m_ThreadHandle,THREAD_PRIORITY_LOWEST);

return true;
}
return false;
}

OK, so here we create the thread and tuck the handle away.

inline bool Thread::WaitForFinish()
{
if(NULL!=m_ThreadHandle)
{
DWORD retVal=::WaitForSingleObject((this->m_ThreadHandle),INFINITE);

And here is the wait that waits forever.

inline bool Thread::Stop()
{
if(NULL!=m_ThreadHandle)
{
if(0<TerminateThread(m_ThreadHandle,0))
{
CloseHandle(m_ThreadHandle);
m_ThreadHandle=NULL;
return true;
}
return false;
}
return true;
}

And here is the reason WHY it waits forever. You terminate the thread, and
then immediately close the handle. As soon as you close the handle, the
handle is invalid, so the WaitForSingleObject is looking at trash memory.
It will never fire.

You should either move the "CloseHandle" and "m_ThreadHandle=NULL" calls to
after the WaitForSingleObject, or put them in the destructor for the class.
When you are doing multithreaded programming, you ALWAYS have to think
about what order things are going to happen.

There is another issue here, however. Using TerminateThread is almost
always a bad idea. It should be considered an "emergency stop", because
the thread does not get a chance to clean itself up properly. A much
better design is to have a flag or an event that you set in the "Stop"
handler, which the thread code will monitor. When the flag or event is
set, the thread exits cleanly, and everybody is happy.
--
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.



Relevant Pages

  • Re: thread synchronization of flag
    ... makes a modification of the "flag" you describe and expects that all ... problem because the sequence cannot be guaranteed. ... In x86 systems, I've never run into the problem. ... terminate processing and the ...
    (comp.programming.threads)
  • Re: AfxEndThread problem
    ... And how can I safely terminate the thread? ... AfxTerminateThread is a shit function and is all ... AfxTerminateThread tries to close the app (it's better to say to ... when main thread decides that the other should stop, set a flag. ...
    (microsoft.public.vc.mfc)
  • Re: Lock a launched application?
    ... > application and restrict all others and terminate them. ... Since your program will only expose approved applications, ... > wait until the application is finished (using WaitForSingleObject) ... Minesweeper since you'll have separate waiting threads for those. ...
    (borland.public.delphi.nativeapi)
  • Re: Yet again - closing a thread
    ... When the client presses a 'STOP' button on the dialog, I want to terminate ... My problem is that there are cases when the WaitForSingleObject call times ... out, and actually never returns WAIT_OBJECT_0, and my application stucks. ... void MyDlg::OnStop ...
    (microsoft.public.vc.mfc)
  • Re: Gforth and Unix signal handling
    ... interruption: longjmp not allowed. ... to terminate I can have a rudimentary handler, ... Forth word that's not exiting graceful, and therefore, you don't want to ... E.g. you could make SIGINT set a flag first. ...
    (comp.lang.forth)