Re: How to cancel an asynchronous GCD block?
Re: How to cancel an asynchronous GCD block?
- Subject: Re: How to cancel an asynchronous GCD block?
- From: Ken Thomases <email@hidden>
- Date: Fri, 01 Jul 2011 03:36:38 -0500
On Jul 1, 2011, at 2:54 AM, Oleg Krupnov wrote:
> I am migrating my code to use GCD blocks instead of NSOperations.
Why?
> But this seems a bit clunky (compared to the elegance of GCD code),
It's pretty much your only option. GCD doesn't have a notion of canceling a task, so you can only set a flag and check it.
> and also I'm not sure if it will be thread-safe to set m_isCancelled
> from the main thread, while the async block may be racing to query it
> (though it should be fine, setting a boolean variable should be
> atomic).
Setting a boolean will be atomic on any likely architecture. However, the technique is safe even if setting the boolean were not atomic. The only transition is from false to true. You're sure it starts out as false. If at any point it evaluates to true, then you know somebody has set it or is in the process of setting it, which means it really is supposed to be true. In theory, yes, the code which is setting it may not have completed, but you don't care.
Atomicity is about avoiding an incomplete or internally-inconsistent result, but that's not possible with a boolean that just transitions once. (It's important that it only transitions once.)
Regards,
Ken
_______________________________________________
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