On Tue, Apr 5, 2011 at 12:10 AM, Chris Hanson <[email protected]> wrote:
> On Apr 4, 2011, at 12:38 PM, Bing Li <[email protected]> wrote:
>
>>        if (nil == defaultMenu)
>>        {
>>                @synchronized(self)
>>                {
>>                        if (nil == defaultMenu)
>
> Don't do this. I don't mean just in Cocoa either, I mean don't do it ever. 
> It's an anti-pattern called "double-checked locking" and it's fatally broken 
> under most languages' memory models.
>
> If you want to initialize a value once-and-only-once, use dispatch_once or 
> (if you need to support an OS version without GCD) use pthread_once.

It's also worth pointing out that NSMenu is not thread safe, and
should never be accessed off the main thread. So the locking is
pointless anyway!
_______________________________________________

Cocoa-dev mailing list ([email protected])

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

This email sent to [email protected]

Reply via email to