thread auto delete and waiting for thread handle to terminate 
Author Message
 thread auto delete and waiting for thread handle to terminate

This is not really a problem but I happen to download a trial copy of
Rational Purify and it told me that I was doing something not quite
kosher. I was doing what I thought was correct, and probably is as
long as the auto delete is OFF. Which it now is.

I had an AfxWinThread running and when my main process wanted to
terminate the thread, it would post a WM_QUIT to the thread and then
wait for single event on the thread's handle before continuing on in.
By default the thread was set for auto delete which means that the
waitforsingle event was waiting on a non-existent handle in a deleted
class. I fixed it by turning off autodelete and manually deleting the
thread class instance once the waitforsingle event returned.

My  question is why did it work before? Shouldn't I have gotten some
sort of access violation once in a while in the waitforsingle event
call?

Thanks,
Kevin



Tue, 06 Jan 2004 08:33:09 GMT  
 thread auto delete and waiting for thread handle to terminate

Quote:

>This is not really a problem but I happen to download a trial copy of
>Rational Purify and it told me that I was doing something not quite
>kosher. I was doing what I thought was correct, and probably is as
>long as the auto delete is OFF. Which it now is.

>I had an AfxWinThread running and when my main process wanted to
>terminate the thread, it would post a WM_QUIT to the thread and then
>wait for single event on the thread's handle before continuing on in.
>By default the thread was set for auto delete which means that the
>waitforsingle event was waiting on a non-existent handle in a deleted
>class. I fixed it by turning off autodelete and manually deleting the
>thread class instance once the waitforsingle event returned.

>My  question is why did it work before? Shouldn't I have gotten some
>sort of access violation once in a while in the waitforsingle event
>call?

You had a race condition before. If the thread self-deleted after you
called WFSO, everything was fine, because calling WFSO increments a
reference count on the handle, so that when the thread closed the
handle, it didn't suddenly become invalid. At least that's what's been
explained to me in the past. However, the current Platform SDK docs
state that closing the handle during a wait is undefined, so you
definitely shouldn't do it, even if it worked in earlier versions of
Windows.

Whatever the case, you're doing the proper thing now. Note that you
should also start the thread suspended, so that you can set
m_bAutoDelete to false before the thread has a chance to run and
immediately exit.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Tue, 06 Jan 2004 10:11:19 GMT  
 thread auto delete and waiting for thread handle to terminate

Quote:

> This is not really a problem but I happen to download a trial copy of
> Rational Purify and it told me that I was doing something not quite
> kosher. I was doing what I thought was correct, and probably is as
> long as the auto delete is OFF. Which it now is.

> I had an AfxWinThread running and when my main process wanted to
> terminate the thread, it would post a WM_QUIT to the thread and then
> wait for single event on the thread's handle before continuing on in.
> By default the thread was set for auto delete which means that the
> waitforsingle event was waiting on a non-existent handle in a deleted
> class. I fixed it by turning off autodelete and manually deleting the
> thread class instance once the waitforsingle event returned.

> My  question is why did it work before? Shouldn't I have gotten some
> sort of access violation once in a while in the waitforsingle event
> call?

> Thanks,
> Kevin

Purify is right, and m_bAutoDelete is a time bomb.  You should get an
invalid return code from WaitForSingleObject, but when I made this
mistake it only failed about once a week.  Low-probability timing
coincidence: Maybe 99.99% of the time the handle is still valid when
WaitForSingleObject executes.

--
Scott McPhillips [VC++ MVP]



Tue, 06 Jan 2004 10:12:20 GMT  
 thread auto delete and waiting for thread handle to terminate
Generally speaking, the following two ways are safe:
1) Let thread quit by itself. It is only used in worker thread.
2) Send a user defined message to the thread and let it quit by itself. It
is often used in user interface thread.
Refer one MVP article by joseph to you.
http://www.pgh.net/~newcomer/uithreads.htm

Hope it helps.

yhhuang
ICQ:91074870



Tue, 06 Jan 2004 10:18:41 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Terminating a Waiting Thread

2. Terminating a Waiting Thread

3. How to wait thread terminate

4. How to wait for a thread to terminate?

5. CWinThread and AfxBeginThread - Thread not auto deleting itself?

6. Thread terminating other threads

7. Threads, Memory Leak, how to wait for thread to close

8. how to terminate a thread from another thread?

9. How to terminate the Child thread from the Parent thread

10. How can i terminate a Worker Thread during a Thread is running?Thank

11. Terminating a thread from another thread

12. Terminating a thread from outside of that thread

 

 
Powered by phpBB® Forum Software