> How can I kill a thread and still have the thread perform cleanup? I
> am not referring to resources that Windows cleans up for me, but
> resources that only the app knows about. The problem is that I need
> to update certain locks in a shared network file if the thread dies,
> but only that thread knows which locks. I can always have another
> thread wait on the first one, but that kind of defeats the purpose and
> breaks encapsulation.
You've found some of the reasons why TerminateThread() shouldn't
be used at all.
In "normal running" I'm using an approach where all blocking I/O
is done asynchronously, and my thread does WaitForMultipleObjects()
on an array that contains the HANDLEs of the file in question, and a
global shutdown event. The return from WaitForMultipleObjects() tells
which "went off", and I do my tidy up if I detect global shutdown.
Note however that this does not protect you from pathological cases.
An example might be a thread suddenly terminating because of a crazed
administrator running something that calls TerminateThread(), or the
thread calling a third party routine from which it never returns.
I've not yet found a "general" approach to OS vs. application code
doing cleanups on NT yet. To be fair, this is not NT's fault. There
is a mix of stuff that the kernel ensures gets done, and other stuff
the application must do for itself in UNIX too. Skilled design
involves always using the kernel stuff such that the system is
never left eg. in deadlock. We just don't have the same level of
experience with NT yet. Of course, excuses don't make for working
systems...
Usual disclaimers, Alan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alan G. Carter - car...@x500.bt.co.uk +44 1344 822816 Voice
BT London Software Engineering Centre +44 1344 822812 Fax
Last Words of James T. Kirk: "Bridge on the Captain!"
32 Belle Vue Terrace, Great Malvern,
Worcestershire, England al...@gid.co.uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You may want to consider calling a cleanup function when you want to
terminate the thread. The cleanup function will in turn call ExitThread()
when it has finished its cleanup.
P.S. Use ExitThread() instead of TerminateThread() whenever possible.
TerminateThread() should only be used if your thread stops responding (
and in a few other rare situations).