On Dec 8, 2012, at 5:27 PM, Richard Heard <heard...@gmail.com> wrote:
> Greg, > > So, from what you are saying, either of these snippets should be valid, right? > >> +(id)sharedInstance{ >> static id _sharedInstance = nil; >> >> if (!_sharedInstance){ >> @synchronized([self class]){ >> if (!_sharedInstance){ >> id sharedInstance = [[super allocWithZone:NULL] init]; >> OSMemoryBarrier(); >> _sharedInstance = sharedInstance; >> } >> } >> } >> >> OSMemoryBarrier(); >> return _sharedInstance; >> } > > vs > >> +(id)sharedInstance{ >> static id _sharedInstance = nil; >> >> static dispatch_once_t onceToken; >> dispatch_once(&onceToken, ^{ >> _sharedInstance = [[super allocWithZone:NULL] init]; >> }); >> >> return _sharedInstance; >> } > > > Any massive advantages / disadvantages with either approach? Use dispatch_once(). It is shorter, simpler, and more obviously correct. The memory barrier implementation may also be correct, but why take the risk? -- Greg Parker gpar...@apple.com Runtime Wrangler _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com