Solved

gracefull NT thread termination upon signal

Posted on 1997-08-28
1
253 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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