Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ShmeeBegek

Multithreading Idiocy!!!

This topic is 5823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Theres ssoooo many functions to CREATE a thread on the windows platform (_beginthread,CreateThread, etc.), but I have not found ONE that can exit a thread from OUTSIDE the thread, there are many for inside (mimmics of ''return'': _endthread,ExitThread, etc.); on WIN32API the function ''CloseHandle'' is supposed to end a thread, but it has no effect! I would really appriciate some help on this, its been troubling me for a while now, trying to write pingers and such without being able to timeout... ~SPH

Share this post


Link to post
Share on other sites
Advertisement
Mind if I ask, what threading system has a killthread function? It''s not a very wise or safe thing to do, amputating part of one''s own process...

pthreads has only pthread_join and pthread_exit for instance, but no pthread_kill afaik. BeOS, same thing. Can''t remember for any other platforms.

Use a thread-safe mechanism to tell a thread to exit, then wait for it to do so (called join or wait, depending on platform).

(My projects and ramblings...)

Share this post


Link to post
Share on other sites
quote:
Original post by LordLethis
I think TerminateThread can be used to close a thread from outside. But MSDN also says it should not be used...


Oops sorry forgot about that one...

yeah - reason not to use it should be obvious from the documentation.

(My projects and ramblings...)

Share this post


Link to post
Share on other sites
CloseHandle only closes the handle to the thread, it will not kill it. Basically it''s like closing your reference to the thread.

Normally when you want to terminate a child thread, you set an event in the main thread. The child thread you want to terminate should check every so often if that event is set, and if it is, exit the child thread by returning from the child thread function. If you want to wait until the child thread exits in the main thread, use WaitForSingleObject on the child thread handle. That will wait until the child thread ends before proceeding.

Share this post


Link to post
Share on other sites
Thanks for that ''TerminateThread'' suggestion, I needed it becouse I am writing a server and need things like ping timeout (if ping takes longer than say 1000 milliseconds then terminate it and return a "took too long" message). If the client is unresponsive, it could otherwise freeze the server thread. I think for situations such as mine were someone actually needs to kill a thread it should be better documented.

Thanks again, ''TerminateThread'' worked fine, ~SPH

Share this post


Link to post
Share on other sites
no, it didn't. TerminateThread has a very high chance of leaking thread-allocated resources until process terminates . it's completely ignorant to use it for anything other than emergencies, which your case is not. there are clean ways to get out of ping time outs, which is to use asynchnonous or nonblocking calls. in fact, i can see no reason why your program should need such a function.

by using TerminateThread, you're shooting yourself in the foot. don't be too happy about it. better change your design to use function calls that can be aborted normally.

[edited by - niyaw on December 7, 2002 4:32:26 PM]

Share this post


Link to post
Share on other sites
I doubt I''ll be able to convince you but what the hell...

Your design is flawed. You should do it the other way around. Have the ping thread exit itself either by watching for the time itself or by checking a flag the mian thread sets. When it exits it returns a status indicating the timeout (or success as appropriate).

The reason you do this is because you don''t know what state the other thread is in. If you call any system api''s they may have allocated resources that will be leaked when you kill the thread. In particular you might leak a critical section which will lock up your whole process. The best part of all this is that it is random. Your server might go for months with no problems then lock up 10 times daily.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Although it seems to work for now, you should seriously consider another design than using TerminateThread().

Once used, your application is broken for several reasons. The stack won''t be freed so you''re leaking memory. DLL''s won''t get notified as they should, which could cause leaks. But worst of all, any locks held by the thread won''t be released. For instance if you''re executing a call to malloc(), and have taken the critical section the heap memory manager uses, further calls to malloc will hang.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!