Re: -forceCancel an NSOperation
Re: -forceCancel an NSOperation
- Subject: Re: -forceCancel an NSOperation
- From: Doug Hill <email@hidden>
- Date: Wed, 01 Mar 2017 14:56:38 -0800
> On Mar 1, 2017, at 2:44 PM, Wim Lewis <email@hidden> wrote:
>
> On Mar 1, 2017, at 2:23 PM, Carl Hoefs <email@hidden> wrote:
>> Normally, that is sufficient. But under some circumstances, it's possible for an executing NSOperation to get hung up doing I/O or for some other reason. No means is given to forcibly stop the execution of an NSOperation, and there's no -thread method to obtain its thread and terminate it.
>
> As Ken Thomases says, this probably can't be done safely in the *general* case.
>
> If you have a specific operation which might need to be asynchronously interrupted, you can probably install a per-thread signal handler (remember to clean it up on completion) and stash a thread identifier somewhere on the NSOperation where your other code can find it. The handler could cancel any blocked syscall and/or set a flag somewhere it can be cheaply checked by cpu-intensive code.
>
> Truly asynchronously terminating a thread is unsafe unless you're very careful about what might be happening in there: lots of library calls can take out global locks temporarily (e.g., anything that uses malloc/free or sends objc messages), and dropping one of those locks will promptly cause your application to hang.
FWIW, pthread_cancel has historically not been implemented in any meaningful way on Darwin and this functionality is generally not useful. For example, there are no cancel points for typical file/network I/O system calls. (e.g. fread/fwrite) AFAIK, there's no way to cancel a synchronous system call via a thread (e.g. without killing the process). I would be interested to hear otherwise.
I think the best you can do, as has been suggested, is to set a flag that the blocked thread can check after it wakes up again so it knows not to continue processing.
Doug Hill
_______________________________________________
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