Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

Question on CloseHandle and Threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 23 May 2006 09:35 GMT
Hi,

from msdn: "Closing a thread handle does not terminate the associated
thread. To remove a thread object, you must terminate the thread, then close
all handles to the thread."

Through the documentation it seems that the thread proc must be exited
*first* and than the CloseHandle should be called on its handle. My problem
is that my threads will exit whenever they want without notifying any other
part of my code. So who will take care of closing their handles? For this
reason I have written the code in such a way that the threads close their
own handle just before they r about to exit from their thread proc. For
example here is what my code looks like.
class anyclass
{
//some where up here is HANDLE m_threadhandle; which contains a valid thread
handle
.....
anyclass::thread_proc()
{
 //  thread functionality here

 ...

 // on the last line before exiting:
 CloseHandle( m_threadhandle );
}
}
Is this ok?

Regards,

Ab.
adebaene@club-internet.fr - 23 May 2006 12:55 GMT
Abubakar a écrit :

> Hi,
>
[quoted text clipped - 4 lines]
> Through the documentation it seems that the thread proc must be exited
> *first* and than the CloseHandle should be called on its handle.

No : a thread is destroyed when it has exited it's thread proc AND all
it's handles are closed. The order in which those events occurs is
irrelevant.

> My problem
> is that my threads will exit whenever they want without notifying any other
> part of my code. So who will take care of closing their handles?

The only reason you may want to keep the handle on a newly created
thread is precisely if you want to wait for this thread to exit (or
manipulate the thread one way or antoher : suspend it, set it's
priority, etc..., but those are usually reserved for debuggers).

If none of your code ever wait for the thread to exit, you can close
the handle returned by CreateThread (or whatever function you use)
immediatly after the CreateThread' call.

> For this
> reason I have written the code in such a way that the threads close their
[quoted text clipped - 16 lines]
> }
> Is this ok?
It is ok but you do not need to make things so complicated...

Arnaud
MVP - VC
Doug Harrison [MVP] - 23 May 2006 17:33 GMT
>Hi,
>
[quoted text clipped - 25 lines]
>}
>Is this ok?

This is essentially what MFC's CWinThread does. Is it a good idea? No. If
the threads die without notifying anyone, it follows that the program can
terminate while they're still running. You should avoid that for reasons
given here in Q1 and Q2:

http://members.cox.net/doug_web/threads.htm

Signature

Doug Harrison
Visual C++ MVP

Abubakar - 26 May 2006 12:15 GMT
Thanks guys for the answers.

Ab.

> >Hi,
> >
[quoted text clipped - 36 lines]
> Doug Harrison
> Visual C++ MVP

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.