static acting like __block in GCD singleton pattern
static acting like __block in GCD singleton pattern
- Subject: static acting like __block in GCD singleton pattern
- From: Matt Neuburg <email@hidden>
- Date: Fri, 28 Jun 2013 17:17:48 -0700
In this sort of well-known code pattern for making a singleton with GCD:
+ (MyClass *) sharedInstance {
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once (&onceToken, ^{
sharedInstance = [MyClass new];
});
return sharedInstance;
}
Why is the block permitted to assign to the variable sharedInstance outside the block? Evidently it is because "static" has an effect like "__block", in that it makes the variable outside the block assignable. But how, exactly? Is it some kind of side effect of being a local static? Thx - m.
--
matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
A fool + a tool + an autorelease pool = cool!
Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do
_______________________________________________
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