Re: How to implement readonly property
Re: How to implement readonly property
- Subject: Re: How to implement readonly property
- From: Steve Sisak <email@hidden>
- Date: Fri, 07 Dec 2012 23:18:34 -0500
At 8:57 PM -0600 12/7/12, Ken Thomases wrote:
> the outer if avoids the overhead of
@synchronized if _someDictionary is already
created -- this is just an optimization
the inner if is necessary to resolve the race
condition if multiple threads make it past the
outer one
This is a classic anti-pattern called
double-checked locking. It is not safe. Don't
rely on it.
https://en.wikipedia.org/wiki/Double-checked_locking
http://erdani.com/publications/DDJ_Jul_Aug_2004_revised.pdf
Hi Ken,
From the first link you cite:
The pattern, when implemented in some
language/hardware combinations, can be unsafe.
At times, it can be considered an
anti-pattern.[2]
That is far different from being a "a classic anti-pattern".
In this example:
1) The language is Obj-C
2) I explicitly used @synchronized(self) and an instance variable
So, in this case, what I'm doing is explicitly supported by the language.
Your second article is explicitly focused on C++
(and singletons) -- it's also dated 2004.
On Mac OS X, the correct implementation of a
singleton is dispatch_once() -- in fact, that is
the function's raison d'ĂȘtre.
So, while I support the position that
double-checked locking can be unsafe in con
language/hardware combination, in this case we're
using language features specifically designed for
the purpose.
That said, it's worth noting that you need to
understand your complier when dealing with
synchronization.
I'm interested if there are an any issued I'm
missing in the Obj-C, @synchronized(self),
instance variable case.
-Steve
_______________________________________________
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