terminating threads

Hi everybody,

I have a question about terminating threads and deallocating memory. I need to kill four threads from the main function and be sure that the memory will be deallocated. I read about TerminateThread function and its problems with memory leaks. I tested the CloseHandle function and it terminates definitely the threads but the memory isn't deallocated. How can I kill threads anytime and ensure that memory will be deallocated? (I'm working with VS 2005 on windows)

Thank you very much.

Radeberger
TerminateThread is a dangerous function that should only be used in the most extreme cases.

When you want a thread to end, set a flag and wait until the thread exits on its own.
Last edited on
ExitThread can be used from thread function but outside of this you may want to use TerminateThread. have a look in msdn because this is the way if you want to exit a thread from outside and close the handle if this is the last thread active. it can have memory leaks as the thread will not get any chance to execute any further.

[Edit]: late.
Last edited on
like helios said:... set up and flag(any kind of var suitable) and make the thread check it repeating... and when it´s set (or unset) the thread should call ExitThread or _endthreadx(depending on how u startet it) and deallocate resources...
Last edited on
you cannot be sure when the thread will exit after seeing the flag.. and if the thread is in a long loop or in deadlock.. you have to wait for ages..i wont say its a bad idea but in some cases it can lead to really big bugs..
I think it's assumed that if you use this method, you know that the thread will be checking it constantly.
its not the thread which will check.. because if it will then it will have a good chance to exit itself..as soon as the thread execute it will be terminated..one can use a combination of (flags + terminate)..even after flagging if the thread doesn't stop, programmer is free to stop it forcibly..

we all are here to give the best options/experiences to others.. members are free to choose the best on.. agree?? :)
agree^^...

@helios : y, i anticipate(excuse me for that if incorrect use of the word) that the thread will check... but when it is blocking itself there won´t be any other solution than terminating it -as far as i know...

so there got to be any kind of routine, that will check the flag, too, and - if needed - terminate the thread after some period of time ... that fpr ui could include another falg, the thread sets while it is cleaning itself up... but that will be kinda confusing when throwing flags around...

to minize the memory-leck you could reduce the threads´ stack size by setting it to a lower value than the standard
The default stack reservation size used by the linker is 1 MB. To specify a different default stack reservation size for all threads and fibers, use the STACKSIZE statement in the module definition (.def) file. The operating system rounds up the specified size to the nearest multiple of the system's allocation granularity (typically 64 KB).
...

[u can set the threads´ stack size within the _beginthread(x)()/CreateThread(x)()-Methods]

there also is an additional memory page for preventing from stack overvlows, so u could set the limits hysteresis close...

sorry for not writing out earlier :P...
Last edited on
You can design thread so that it is possible to terminate it from outside at anytime if you add all it resources to a container so outside thread can free all of them after forcing the termination.

Better solution is to use non-blocking system calls. In unix world you can make thread not to run until some event happens that it has to work on. Events can be anything from some network data arriving to program setting termination flag for thread. I think same is possible in windows using WaitForMultipleObjects but I don't remember the details.
what do you mean with "designing thread"?... as long as u start a thread it will receive its own stack... and when u terminate it the memory pages are still locked versus reusage...
Topic archived. No new replies allowed.