• 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: Ken Thomases <email@hidden>
  • Date: Fri, 09 Dec 2011 00:55:00 -0600

On Dec 8, 2011, at 11:18 PM, Abdul Sowayan wrote:

> Hi Ben,
>
>> I'm rather confused by the commonly used implementation of singletons. (Ignoring the fact that perhaps I shouldn't be using singletons) I would appreciate you view on this.
>>
>> Normally I implement a singleton such as..
>>
>> static MyClass *sharedInstance = nil;
>> + (MyClass *)sharedInstance
>> {
>> 	@synchronized(self)
>>   {
>> 		if (sharedInstance == nil)
>> 			sharedInstance = [[self alloc] init];
>>  }
>> 	return(sharedInstance);
>> }
>
> 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>.

Regards,
Ken

_______________________________________________

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

  • Follow-Ups:
    • Re: Singletons with ARC
      • From: Uli Kusterer <email@hidden>
References: 
 >Singletons with ARC (From: Ben <email@hidden>)
 >RE: Singletons with ARC (From: Abdul Sowayan <email@hidden>)

  • Prev by Date: Re: Strange behivor when our document-based Cocoa application launch on lion
  • Next by Date: Re: NSIndexSet of NSButtonCells in NSOutlineView
  • Previous by thread: RE: Singletons with ARC
  • Next by thread: Re: Singletons with ARC
  • Index(es):
    • Date
    • Thread