Solved

gracefull NT thread termination upon signal

Posted on 1997-08-28
1
247 Views
Last Modified: 2013-12-03
This problem affects NT4.0 (with Watcom 10.6 but should be compiler independent).
Here is the question: How to terminate a thread gracefully by cleaning up the stack instead of having a growing stack, e.g. by use of TerminateThread(hdl).

All our attempts to terminate a thread other than letting EndThread()initiating the return to the call lead to a growing overall memory consumption of the application. The disadvantage of EndTread() is that it can´t be used directly to terminate the thread from outside.
The only awkward solution that we additionally fear would be to use something like a flag that would have to be polled frequently.
 
Our approach to test is: malloc for the stack of a thread and to pass the pointer received from malloc within BeginTread(), alternatively calling BeginThread and feeding Zeros as arguments only. The thread itself does a simple delay (for test purposes), nothing else. Now we tested 2 options:
1) let the thread terminate normally without any signal
2) terminate by use of TerminateThread()
Option 1 seems to be ok, option 2 causes a increase in memory allocation (although freeing the malloced memory if necessary).

We know that signalling is poor under NT but is it really so poor that there is no solution? How is the rest of the NT programmers solving this kind of problem?
0
Comment
Question by:august97
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
md041797 earned 25 total points
ID: 1405009
I presume you mean ExitThread instead of EndThread.

The only solution is the akward one.  
1) Create an event object using the CreateEvent function.
2) Create the threads.
3) Each thread monitors the event state by calling the WaitForSingleObject function. Use a wait time-out interval of zero.
4) Each thread terminates its own execution when the event is set to the signaled state (WaitForSingleObject returns WAIT_OBJECT_0).

A WinNT thread needs to be basically a loop construct:

while (!ReadyToQuit) {
 do more work;
 };

Note that if your thread is sleeping waiting on an event, you can use WaitForMultipleEvents and then include the "kill" event as a waiting event.  This would essentially prevent the "polling" problem because the thread only polls when it is doing some work anyway.


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 341
Clean Uninstall of VS Professional 2015 5 60
Problem to file 3 92
Dialogbox API leak? 18 109
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question