Re: Is Apple's singleton sample code correct?
Re: Is Apple's singleton sample code correct?
- Subject: Re: Is Apple's singleton sample code correct?
- From: Shawn Erickson <email@hidden>
- Date: Sat, 26 Nov 2005 07:23:11 -0800
On Nov 26, 2005, at 7:08 AM, Shawn Erickson wrote:
On Nov 26, 2005, at 5:28 AM, Uli Kusterer wrote:
Am 26.11.2005 um 04:50 schrieb David Gimeno Gost:
+ (id) allocWithZone: (NSZone*) zone
{
@synchronized( self ) {
if ( sharedInstance == nil ) {
sharedInstance = [super allocWithZone: zone];
} else {
[sharedInstance retain];
}
}
return sharedInstance;
}
Errm ... good start, but you meant to be @synchronized() on some
other object. If you synchronize to self, two threads create a
"self", and both sync to their own "self"s, so you effectively get
the same result as if you didn't have the @synchronized call in
there at all. You need an explicit NSLock here, or one shared
object that you know you can rely on already existing.
I believe you are fine here with the locking in the above because
this is a class method, self is related to the class which their
should only be one in the current runtime instance for the process
(not sure if runtime loading of a class could present a window, I
have noticed one).
Sorry meant "I haven't noticed one" in the above... /me reaches for
coffee.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden