Closing a thread handle (WIN 32).

Started by
15 comments, last by ANSI2000 20 years, 5 months ago
Well one method that could work really well is to create a thread that will check the status of all other threads using WaitForObject... and close handles as needed. This status thread will only exit at the shutdown process of the application and it''s handle can be closed then...

I was thinking of writting a small Thread Manager class using sort of a command pattern. Where each thread can be passed an "object" and the thread can then call something like object->execute();... And the thread manager can then handle all the cleanup etc.. of the threads...
Advertisement
quote:Original post by Lorenzo
TerminateProcess is always safe. When the process terminates, all the resources are freed (including its address space, threads and kernel structures)


Not according to Microsoft themselves.

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
The warnings about TerminateProcess is just Microsoft covering it''s ass because developers do dumb things. If the app has cached data that data may be lost. If the app requires some sort of network handshake or the nuclear reactor explodes that handshake isn''t going to happen. etc, etc. Basically the deal is that your app will get cleaned up but another other processes that rely on your app shutting down gracefully are out of luck. Developers are supposed to handle ungraceful shutdowns but often don''t.

The warnings about TerminateThread on the other hand are real. Don''t ever use it for any reason. It''s better to shutdown your app and make the user restart it rather than gambling on TerminateThread working and leaving your app in a clean state.


-Mike
I concur.

TerminateThread reeks of non-deterministic behavior. I can''t see how this is a good thing at all.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:TerminateProcess is always safe.

No it isn''t.

quote:When the process terminates, all the resources are freed (including its address space, threads and kernel structures)

And if the program is manipulating some cross-process shared state at the time it''s terminated?

The kernels not going to unfuck the data for you.

TerminateProcess() is not safe.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:The warnings about TerminateProcess is just Microsoft covering it''s ass because developers do dumb things.

No, they''re MS making the dangers of the TerminateProcess() function absolutely clear.

An app can''t handle TerminateProcess() gracefully -- it doesn''t get the chance -- so as a result it can, for instance, leave files partially written and corrupt shared state. There''s nothing you can do about this, so the only answer is don''t call TerminateProcess(). It''s dangerous for all the same reasons that TerminateThread() is dangerous.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Thank you DrPizza for your support...

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement