Re: Programming Context Menu
Re: Programming Context Menu
- Subject: Re: Programming Context Menu
- From: "Stephen J. Butler" <email@hidden>
- Date: Tue, 05 Apr 2011 00:57:13 -0500
On Tue, Apr 5, 2011 at 12:10 AM, Chris Hanson <email@hidden> wrote:
> On Apr 4, 2011, at 12:38 PM, Bing Li <email@hidden> 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@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