[libdispatch-dev] dispatch_once_t must have global or static scope?

Daniel A. Steffen dsteffen at apple.com
Thu Jul 26 10:57:07 PDT 2012


On Jul 26, 2012, at 10:39, Michael Ash <michael.ash at gmail.com> wrote:

> On Jul 26, 2012, at 1:30 PM, Daniel A. Steffen wrote:
> 
>> the main issue with dynamically allocated storage is safe initialization of the dispatch_once token value.
>> E.g. for an object with a dispatch_once_t instance variable, depending on how that object is made visible to other threads after creation, a memory barrier will be required on some platforms to ensure the zeroing of the dynamically allocated storage is visible to other CPUs before the object pointer.
> 
> Excellent! That's exactly the sort of thing I was hoping I hadn't thought of before, in terms of understanding the limitation.
> 
> Do I understand it correctly, then, that it's safe as an Objective-C instance variable, as long as you take the standard necessary measures before letting a pointer to that ivar leak out to other threads (memory barriers or synchronization to ensure that other threads see fully initialized object contents)?

the only other potential issue could be alignment (since the token is operated on with atomic instructions), I don't know if ObjC actually makes any guarantees about ivar storage layout

> Would it be possible to have a brief discussion of this added to the documentation? This may be a bit beyond the level that the docs intend to cover, though....

indeed that is probably why the documentation on the developer site doesn't go into it, but please file a bugreport about it anyway

Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/libdispatch-dev/attachments/20120726/aed7b2d5/attachment-0001.html>


More information about the libdispatch-dev mailing list