473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question on CloseHandle and Threads

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.
May 23 '06 #1
3 2338

Abubakar a écrit :
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.
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
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?

It is ok but you do not need to make things so complicated...

Arnaud
MVP - VC

May 23 '06 #2
On Tue, 23 May 2006 13:35:14 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
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?


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

--
Doug Harrison
Visual C++ MVP
May 23 '06 #3
Thanks guys for the answers.

Ab.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:l2********************************@4ax.com...
On Tue, 23 May 2006 13:35:14 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
Hi,

from msdn: "Closing a thread handle does not terminate the associated
thread. To remove a thread object, you must terminate the thread, then closeall 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 problemis that my threads will exit whenever they want without notifying any otherpart 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 threadhandle
.....
anyclass::thread_proc()
{
// thread functionality here

...

// on the last line before exiting:
CloseHandle( m_threadhandle );
}
}
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

--
Doug Harrison
Visual C++ MVP

May 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
16
by: dario | last post by:
Hi, Im new on phyton programming. On my GPRS modem with embedded Phyton 1.5.2+ version, I have to receive a string from serial port and after send this one enclosed in an e-mail. All OK if the...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
4
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
0
by: Nick | last post by:
hi,all I have a question about named pipe. How does the pipe server know the client closed the connection? We can refer the example for CreateNamedPipe() function in MSDN. In block mode, once...
0
by: Steve N. | last post by:
I'm looking to do some basic (to begin with anyway) Serial IO in a __gc class. I use this code open a port: m_hSerialComm = CreateFile(LpstrPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING,...
4
by: Vikas | last post by:
Hi, I have a problem related to CloseHandle. While calling to CloseHandle(hHandle) sometimes the code gets hang up, control doesnt return back from CloseHandle. The handle passed to the method...
3
by: Chris Roth | last post by:
I'm using VS.net 7.1 on Windows XP. I have a class that hold a container of doubles. One function (foo) for the class calls a sub-function (bar) for each of the doubles. I'd like to...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.