Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread Termination Cleanup

0 views
Skip to first unread message

Jim Beveridge

unread,
Apr 12, 1995, 3:00:00 AM4/12/95
to
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.

I tried putting in a try/finally handler, but the "finally" case never
gets called if TerminateThread is used, and I cannot find any other
call that will allow one thread to terminate another. The UNIX
equivalent for this problem is kill() from one process and
signal(SIGTERM, xxx) in the other process.

The same problem comes up in this example. One thread is blocked on a
Winsock call. The user presses the cancel button, so the main thread
wants to kill the blocked thread. The blocked thread needs to send
a CANCEL message to the server before it terminates, which requires
the blocked thread to trap the termination before it dies.

Ideally I need a solution that works in either a windowed or a
console application. Any suggestions would be appreciated.


Thanks,

Jim Beveridge

david....@cssd.octel.com

unread,
Apr 18, 1995, 3:00:00 AM4/18/95
to
In article <3mh66u$1...@enterprise.turningpoint.com>
Jim Beveridge <ji...@turningpoint.com> wrote:

> 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.

Alan Carter

unread,
Apr 18, 1995, 3:00:00 AM4/18/95
to
Jim Beveridge <ji...@turningpoint.com> wrote:
>
> 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.
>
> I tried putting in a try/finally handler, but the "finally" case never
> gets called if TerminateThread is used, and I cannot find any other
> call that will allow one thread to terminate another. The UNIX
> equivalent for this problem is kill() from one process and
> signal(SIGTERM, xxx) in the other process.
>
> The same problem comes up in this example. One thread is blocked on a
> Winsock call. The user presses the cancel button, so the main thread
> wants to kill the blocked thread. The blocked thread needs to send
> a CANCEL message to the server before it terminates, which requires
> the blocked thread to trap the termination before it dies.
>
> Ideally I need a solution that works in either a windowed or a
> console application. Any suggestions would be appreciated.

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fernando Chapa

unread,
Apr 24, 1995, 3:00:00 AM4/24/95
to
>Jim Beveridge <ji...@turningpoint.com> wrote:
>
>> 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.

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).


0 new messages