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::threa d_proc()
{
// thread functionality here
...
// on the last line before exiting:
CloseHandle( m_threadhandle );
}
}
Is this ok?
Regards,
Ab. 3 2371
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::threa d_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
On Tue, 23 May 2006 13:35:14 +0500, "Abubakar" <em**********@y ahoo.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::thre ad_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
Thanks guys for the answers.
Ab.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:l2******** *************** *********@4ax.c om... On Tue, 23 May 2006 13:35:14 +0500, "Abubakar" <em**********@y ahoo.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::thre ad_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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 sample at hand is
concerned about threads, it could be related to widgets for the
purpose of this question (that's so I don't get flamed and told to go
to comp.programming.threads :)
|
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 string is directly generated in the code. But it doesn't
works if I wait for this inside a 'while' loop. This is the simple
code:
global stringZVEI
|
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 threads of a multithreaded
program using embedded SQL. Since the threads do not need to share
transaction scopes, the sqleAttachToCtx family of APIs do not seem to be
necessary.
<quote>
In the default implementation of threaded applications against...
|
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 the heap and release the memory when it's deleted from the hash table so that GC recollection does not need to run that frequently.
I read up examples about Dispose method in MSDN. What I find very strange is that it's all talking about how to...
|
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 ConnectNamedPipe() returns, it means the client
called CreateFile(),
and after finish reading and writing, client call CloseHandle(), at
this point, how does the pipe server know the pipe has been closed? And
if client did not call CloseHandle(),...
| |
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, FILE_FLAG_NO_BUFFERING, 0);
I was able to write successfully, but the following code hangs:
if(m_hSerialComm != INVALID_HANDLE_VALUE) {
CloseHandle(m_hSerialComm);
|
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 points
to a NIC on my machine. When the handle was opened the NIC was enable
but later I disable the NIC and then call the CloseHandle. The exe
just hungs up, and cant be killed. I need to restart my machine to
overcome the problem.
Can anyone...
|
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 multithread foo so that the bar sub-functions can run on multiple
threads. I'd like to imlpement this with _beginthreadex as I'm using
std::vector. Please provide some working code around the following details:
#include <windows.h // for HANDLE...
|
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.
Threading is a new concept for me to implement. Here is my problem.
I have a system that receives xml files and records their file
locations in a database. I can potentially receive thousands,
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |