Re: Singletons and Threads
Re: Singletons and Threads
- Subject: Re: Singletons and Threads
- From: Milo Bird <email@hidden>
- Date: Sun, 16 Sep 2007 14:17:13 +0100
If I recall correctly when using @synchronized() you should use a
*specific* object, not a variable such as self. I usually write my
singleton class methods to use [MySingletonClass class] as the
semaphore.
But self IS a specific object. In the context of a class method, self
is the class object itself. So, synchronizing on self is the same as
synchronizing on [self class], which is the same as synchronizing on
[MySingletonClass class].
In some other situations, yes, it is important to remember that
@synchronized acts on the value of the pointer you pass it rather
than just the name of the variable. For instance, this accessor would
not be properly thread safe, because the value of the semaphore is
actually changing within the critical code:
- (void)setFoo:(id)newFoo;
{
@synchronized(_foo)
{
if (_foo == newFoo)
return;
[_foo release];
_foo = [newFoo retain];
}
}
_______________________________________________
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