Re: Singletons and Threads
Re: Singletons and Threads
- Subject: Re: Singletons and Threads
- From: "Randall Wood" <email@hidden>
- Date: Fri, 14 Sep 2007 16:24:27 -0400
This is the code I have for ensuring that the class is a singleton:
static MPInterpreter *sharedMPInterpreter = nil;
@implementation MPInterpreter
- (id) init {
if (self = [super init]) {
_interpreter = Tcl_CreateInterp();
if(_interpreter == NULL) {
NSLog(@"Error in Tcl_CreateInterp, aborting.");
return Nil;
}
if(Tcl_Init(_interpreter) == TCL_ERROR) {
NSLog(@"Error in Tcl Init: %s", Tcl_GetStringResult(_interpreter));
return Nil;
}
if( Tcl_EvalFile(_interpreter, [[[NSBundle mainBundle] pathForResource:
@"init" ofType:@"tcl"] UTF8String]) != TCL_OK) {
NSLog(@"Error in Tcl_EvalFile: %s", Tcl_GetStringResult(_interpreter));
return Nil;
}
}
return self;
}
+ (MPInterpreter*)sharedInterpreter {
@synchronized(self) {
if (sharedMPInterpreter == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedMPInterpreter;
}
+ (id)allocWithZone:(NSZone*)zone {
@synchronized(self) {
if (sharedMPInterpreter == nil) {
sharedMPInterpreter = [super allocWithZone:zone];
return sharedMPInterpreter; // assignment and return on first allocation
}
}
return nil; // subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone*)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
It is almost straight from an example in (I think) the Apple Developer site.
I would like to be able to have code that ensures that a only a single
instance exists within each thread.
On 9/14/07, Clark Cox <email@hidden> wrote:
>
> On 9/14/07, Randall Wood <email@hidden> wrote:
> > If I have a class that is a singleton, and create the first instance in
> a
> > thread using a standard [sharedInstance] method, will calling
> > [sharedInstance] in another thread use that single instance, or will
> each
> > thread get its own instance of the class?
> > Searching about, I can't find an explicit answer to this question.
>
> That all depends on how you've written your class. You could write it
> to function either way. Odds are, if you've written it the most
> straightforward way (i.e. storing a pointer to the singleton in a
> static variable), all threads will share the same instance.
>
> As a side note, be sure that you've adequately protected your
> sharedInstance method from threading problems. It would be best if you
> posted your code, without that, any answer you get will just be
> conjecture.
>
>
> --
> Clark S. Cox III
> email@hidden
>
--
Randall Wood
email@hidden
"The rules are simple: The ball is round. The game lasts 90 minutes. All the
rest is just philosophy."
_______________________________________________
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