• 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 and Threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Singletons and Threads


  • Subject: Re: Singletons and Threads
  • From: Wincent Colaiuta <email@hidden>
  • Date: Sun, 16 Sep 2007 16:45:18 +0200

El 16/9/2007, a las 15:17, Milo Bird escribió:

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].

Just to nitpick, there is one case where [self class] is not the same as [MySingletonClass class], and that is when working with subclasses: if the subclass calls "super" in a class method defined in the superclass then "self" will be a pointer to the instance of the subclass, not the superclass.


So if you control things or know your singleton class will never be subclassed, then you know can treat "self" (when used in class methods) and [MySingletonClass self] as equivalent. But if you want to allow for the possibility of subclassing then you have to bear in mind this issue.

Having said that, in my own code base I don't have any singleton classes that are designed with subclassing in mind. But conceivably it's something you could do.

Cheers,
Wincent

_______________________________________________

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 and Threads (From: "Randall Wood" <email@hidden>)
 >Re: Singletons and Threads (From: "Clark Cox" <email@hidden>)
 >Re: Singletons and Threads (From: "Randall Wood" <email@hidden>)
 >Re: Singletons and Threads (From: PGM <email@hidden>)
 >Re: Singletons and Threads (From: Chris Hanson <email@hidden>)
 >Re: Singletons and Threads (From: "Shawn Erickson" <email@hidden>)
 >Re: Singletons and Threads (From: Keith Duncan <email@hidden>)
 >Re: Singletons and Threads (From: Milo Bird <email@hidden>)

  • Prev by Date: How to avoid excessive hard coding when programatically inserting Core Data entity ?
  • Next by Date: Re: How to avoid excessive hard coding when programatically inserting Core Data entity ?
  • Previous by thread: Re: Singletons and Threads
  • Next by thread: Re: Singletons and Threads
  • Index(es):
    • Date
    • Thread