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
Solved

gracefull NT thread termination upon signal

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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