Solved

gracefull NT thread termination upon signal

Posted on 1997-08-28
1
238 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
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now