• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Singletons with ARC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Singletons with ARC


  • Subject: Re: Singletons with ARC
  • From: Andreas Grosam <email@hidden>
  • Date: Fri, 09 Dec 2011 09:08:41 +0100

On Dec 9, 2011, at 8:11 AM, Uli Kusterer wrote:

> On 09.12.2011, at 07:55, Ken Thomases wrote:
>> On Dec 8, 2011, at 11:18 PM, Abdul Sowayan wrote:
>>> The above will still work in an ARC environment. It is rather inefficient, however. For details please check the "Double Check Locking Optimization pattern":
>>> http://www.cs.wustl.edu/~schmidt/PDF/DC-Locking.pdf
>>
>> Double-checked locking is broken.  It is an anti-pattern in many languages, including the C family under most common implementations.  Don't use it.  Google it if you want to confirm.  One reference: <http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>.
>
>
> Is this an issue that just exists when you want to write against ANSI C, or is this an actual, practical concern when writing on the Mac, for MacOS or iOS? Does anyone have information on the actual memory model implemented in Mac C, ObjC and C++ compilers that reinforce this?

A conforming C or C++ compiler may produce code for this pattern which is ultimately not thread safe. (And an optimizing compiler likely will very likely produce unsafe code).

C and C++ have no notion of threads. Thus, it seems reasonable that a code generator is allowed to set a pointer variable to a partially constructed object. Even using the keyword "volatile" as a modifier for the pointer variable may not make this thread safe.

For more information, please take a look into  <http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf>

>
> Just trying to figure out whether I have to change a lot of existing Mac and iOS code now, or whether it suffices to be aware of the problem and not write it in new code and stuff that gets ported.

Well, the special feature of race conditions is, that they happen rarely. When it never occurred so far, it doesn't prove that there is no race condition, and that it can't occur in the next second.

I would use dispatch_once if possible. Otherwise, I would use a mutex/critical section protecting the relevant resource/code. If you need to access the singleton in a tight loop, just assign it a local variable once.


Andreas

_______________________________________________

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

References: 
 >Singletons with ARC (From: Ben <email@hidden>)
 >RE: Singletons with ARC (From: Abdul Sowayan <email@hidden>)
 >Re: Singletons with ARC (From: Ken Thomases <email@hidden>)
 >Re: Singletons with ARC (From: Uli Kusterer <email@hidden>)

  • Prev by Date: Re: Singletons with ARC
  • Next by Date: Re: Singletons with ARC
  • Previous by thread: Re: Singletons with ARC
  • Next by thread: Re: Singletons with ARC
  • Index(es):
    • Date
    • Thread