Looking at the docs, dispatch_once takes care of the synchronization for you:

https://developer.apple.com/library/mac/ipad/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

It should therefore be thread safe to use without any additional 
synchronization code. 

Sent from my New iPad. I blame all typos on the Fruit Company. May have been 
dictated. 

On 2012-11-12, at 7:56 AM, "Gerriet M. Denkmann" <gerr...@mdenkmann.de> wrote:

> I have a property:
> 
> @property (readonly)  NSDictionary *someDictionary;        
> 
> This property should be computed on demand, and should be accessible by 
> several threads.
> 
> My current implementation is:
> 
> - (NSDictionary *)someDictionary;
> {
>    static NSDictionary *someDictionary;
>    static dispatch_once_t justOnce;
>    dispatch_once( &justOnce, ^
>        {
>            // create a temp dictionary (might take some time)
>            someDictionary = temp;
>        }
>    );
> 
>    return someDictionary;
> }
> 
> The first thread which needs someDictionary will trigger its creation. Ok.
> 
> But what happens when another thread wants to access someDictionary while it 
> is still being created? I guess it will receive just nil.
> This would be not correct; it really should wait until the dictionary is 
> ready.
> 
> How to achieve this? Use a lock? Use @synchronize?
> 
> 10.8.2 with Arc.
> 
> 
> Gerriet.
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> https://lists.apple.com/mailman/options/cocoa-dev/mtabini%40me.com
> 
> This email sent to mtab...@me.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to