• 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: How to implement readonly property
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to implement readonly property


  • Subject: Re: How to implement readonly property
  • From: Ken Thomases <email@hidden>
  • Date: Mon, 12 Nov 2012 10:36:58 -0600

On Nov 12, 2012, at 8:41 AM, Joerg Simon wrote:

> On Nov 12, 2012, at 3:33 PM, Tom Davie <email@hidden> wrote:
>
>> On 12 Nov 2012, at 14:18, Joerg Simon <email@hidden> wrote:
>>
>>> You can use dispatch_sync. The blog post of oliver dobnigg (cocoanetics) summs that up quite nicely:
>>> http://www.cocoanetics.com/2012/02/threadsafe-lazy-property-initialization/
>>
>> Or you can use dispatch_once, but make sure the once token is an ivar, unlike I did.
>
> As you can read in the blog too, the developer documentation of dispatch_once states:
>
> "The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined."
>
> so, no, you can not. Actually it works most of the time, but you can not rely on it...

Far be it from me to discourage people from paying attention to the docs, but I'm pretty sure that the docs are excessively restrictive in this case.

From working with similar constructs in other APIs, I believe the actual requirements are:

1) All of the threads which are to coordinate on doing a task exactly once must be referring to the same storage for the once predicate.

2) The predicate storage has to be guaranteed to have been allocated and initialized to zero before any threads access it.

3) The storage must not be deallocated until after it is guaranteed that no threads will reference it again.

Obviously, automatic storage violates rule 1.  Most schemes which try to dynamically allocate the storage just before it's needed would normally run into the same race condition that dispatch_once() is trying to solve.  And the most common anticipated use case is for ensuring that a given task is only performed once, globally.  So, avoiding dynamic storage is a nice simple rule.  (It's also easy to forget to set dynamically allocated storage to zero before using it.)

But an instance variable still satisfies all three requirements for the case where a task needs to be performed only once per instance.

And the blog's speculation that there's some special characteristic of statically allocated memory vs. dynamically allocated memory that is important to dispatch_once()'s synchronization strikes me as very, very improbable.

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: How to implement readonly property
      • From: Greg Parker <email@hidden>
References: 
 >How to implement readonly property (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to implement readonly property (From: Tom Davie <email@hidden>)
 >Re: How to implement readonly property (From: Marco Tabini <email@hidden>)
 >Re: How to implement readonly property (From: Tom Davie <email@hidden>)
 >Re: How to implement readonly property (From: Joerg Simon <email@hidden>)
 >Re: How to implement readonly property (From: Tom Davie <email@hidden>)
 >Re: How to implement readonly property (From: Joerg Simon <email@hidden>)

  • Prev by Date: Re: Adding UINavBar to split view detail in storyboard?
  • Next by Date: [Q] Difference in terms of when it sense that it's rotated?
  • Previous by thread: Re: How to implement readonly property
  • Next by thread: Re: How to implement readonly property
  • Index(es):
    • Date
    • Thread