Re: Killing a Thread
Re: Killing a Thread
- Subject: Re: Killing a Thread
- From: "Michael Ash" <email@hidden>
- Date: Tue, 25 Nov 2008 13:16:47 -0500
On Tue, Nov 25, 2008 at 11:31 AM, Scott Ribe <email@hidden> wrote:
>> I'm new to thread programming. I need to kill the thread in a void function.
>
> Let the function end. It's that simple.
>
>> How can I do this?
>
> You can't just kill a thread. You can let the function return when it's
> finished. You can arrange some way to signal it to exit, and have it return
> when it sees the signal (and this includes the POSIX function to "kill" a
> thread). But there is no function to simply terminate one thread from
> another, because there is NO WAY to create a function to do that safely.
Actually there is such a function. Check out pthread_setcanceltype()
and the PTHREAD_CANCEL_ASYNCHRONOUS option, which does exactly that
when used in conjunction with the pthread_cancel() function.
However you should pretty much never use this. It is extremely
difficult to write asynchronous-cancel-safe code. Among other things,
you must disable thread cancellation before invoking *any* external
code that isn't guaranteed to be cancel-safe (which is basically all
of it), including things like objc_msgSend which means you can't
safely do any ObjC messaging while cancellation is enabled.
Thread cancellation is available but it's very bad mojo and is rarely
the correct solution to anything, pretty much as you said.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden