Re: Killing a thread by force
Re: Killing a thread by force
- Subject: Re: Killing a thread by force
- From: Dan Sugalski <email@hidden>
- Date: Tue, 20 May 2003 16:02:44 -0400
At 11:12 AM +0530 5/20/03, Raja Mallick wrote:
The thread is working fine. But my requirement is when a button, say cancel is
clicked, the longTask method will quit and the thread will be killed. Can
anybody give me some idea, how can I stop the method longTask and kill the
thread for a button click?
The best way to do this is to have a shared word somewhere that the
long-running task checks and, if it's non-zero, the thread shuts
itself down.
Actually killing a thread externally is generally a dangerous thing,
since the thread may well be in the middle of altering some critical
resource (like, say, allocating memory) that will then be left in an
inconsistent state, and your program will die a horrible death. Or
the thread will die with a locked mutex and your process will
deadlock, which is also a bad thing.
There are ways around this, by protecting critical sections of code,
but the problem there is twofold--you will inevitably miss something,
and the cost to set up and tear down the protections around the
critical sections often ends up being more expensive than just
checking the flag at spots where it's safe to exit. (Plus, of course,
you often want the thread to do at least a little cleanup, which it
can't do if you've shot it...)
--
Dan
--------------------------------------"it's like this"-------------------
Dan Sugalski even samurai
email@hidden have teddy bears and even
teddy bears get drunk
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.