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

Daniel A. Steffen dsteffen at apple.com
Thu Jul 26 10:30:23 PDT 2012

Hi Mike,

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.


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

> The GCD reference on developer.apple.com says, regarding dispatch_once_t:
> "Variables of this type must have global or static scope. The result of using this type with automatic or dynamic allocation is undefined."
> https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
> However, I can't find any explanation of why that would be the case, either in the documentation or the libdispatch code. Does anyone have any insight to shed on this? It's occasionally useful to have a dispatch_once_t member variable in an Objective-C object, and it would be nice to know if that's actually OK or not. I have a hard time figuring out how GCD could possibly care about the storage scope of the variable, but it's possible that I'm overlooking something.
> Mike
> _______________________________________________
> libdispatch-dev mailing list
> libdispatch-dev at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo/libdispatch-dev

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

More information about the libdispatch-dev mailing list