Re: static acting like __block in GCD singleton pattern
Re: static acting like __block in GCD singleton pattern
- Subject: Re: static acting like __block in GCD singleton pattern
- From: Matt Neuburg <email@hidden>
- Date: Sat, 29 Jun 2013 10:11:57 -0700
That is *extremely* clear - thanks! m.
On Jun 29, 2013, at 9:42 AM, Jens Alfke <email@hidden> wrote:
>
> On Jun 29, 2013, at 9:20 AM, Matt Neuburg <email@hidden> wrote:
>
>> I'm trying to come up with a pithy explanation, suitable for beginners, of why a "static" variable doesn't need a "__block" specifier in order for a block to assign to it. The word "captured", as you suggest, may be useful here, since this embodies the notion that an automatic variable is copied as const (and so can't be assigned to) in the context of the block. Thx - m.
>
> The block is going to run in its own stack frame, like a regular function. But there’s no normal way for a function to access a local variable of another function (the enclosing function of the block.) So enclosing variables used by the block either have to be copied in the block’s stack frame, or referenced through pointers back to the enclosing stack frame. The latter is clearly more expensive, both because of the pointer dereference and because there’s extra work that has to be done to rescue that variable if the block can survive past the return of its caller (because the stack frame will be gone later on.) That’s why it’s optional, and you have to use __block to get that behavior.
>
> None of this is an issue with a static or global variable because it doesn’t live on the stack or belong to some enclosing function. It’s just got a single location that’s valid for the lifetime of the program. (Even if you declared it inside a function: that only affects where the variable can be referred to, not its lifetime.)
>
> —Jens
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
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