• 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: Global in NSApplication
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global in NSApplication


  • Subject: Re: Global in NSApplication
  • From: Graham Cox <email@hidden>
  • Date: Thu, 28 Jan 2010 08:47:48 +1100

On 28/01/2010, at 3:37 AM, Scott Ribe wrote:

> Of course this code does not necessarily *prevent* you from making multiple
> Debug instances, but neither does the code Graham posted if you try to call
> [Debug sharedDebug] from multiple threads.


Calling a singleton method from multiple threads does not make multiple instances. It returns the same shared instance regardless (the 'static' keyword does not create thread local storage*). In fact that might be cause for concern if you are using it from multiple threads - you need to design the class itself to be thread-safe.

If you do want multiple instances, a simple alloc/init when needed is all you need to do, but if you truly want to enforce a single instance under all possible conditions, you'd need to override -init and -copyWithZone: and probably others to always return the singleton.

*GCC supports thread-local storage using the __thread keyword. Use that instead of 'static' and you will get a separate instance per thread, though as a GCC feature not a language feature the code is less portable.

--Graham


_______________________________________________

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: Global in NSApplication
      • From: Scott Ribe <email@hidden>
References: 
 >Re: Global in NSApplication (From: Scott Ribe <email@hidden>)

  • Prev by Date: Re: PDF View - smooth text?
  • Next by Date: Re: Global in NSApplication
  • Previous by thread: Re: Global in NSApplication
  • Next by thread: Re: Global in NSApplication
  • Index(es):
    • Date
    • Thread