Skip to content

Instantly share code, notes, and snippets.

@gcox
Created March 2, 2012 20:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcox/1961202 to your computer and use it in GitHub Desktop.
Save gcox/1961202 to your computer and use it in GitHub Desktop.
Thread-safe singleton pattern using dispatch_once
@interface Something : NSObject
+(Something *)somethingSingleton;
@end
@implementation Something
+(Something *)somethingSingleton {
static Something *something = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
something = [[self alloc] init];
});
return something;
}
@end
@gcox
Copy link
Author

gcox commented Mar 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment