On Dec 8, 2012, at 10:06 AM, Steve Sisak <sgs-li...@codewell.com> wrote:

> At 8:35 AM -0800 12/8/12, Kyle Sluder wrote:
>> On Dec 7, 2012, at 8:38 PM, Marco S Hyman <m...@snafu.org> wrote:
>> 
>>> On Dec 7, 2012, at 8:18 PM, Steve Sisak <sgs-li...@codewell.com> wrote:
>>> 
>>>> I'm interested if there are an any issued I'm missing in the Obj-C, 
>>>> @synchronized(self), instance variable case.
>>> 
>>> 
>>> Your pattern can fail if this line
>>>        _someDictionary = temp;
>>> isn't atomic.
>> 
>> The real issue with double-checked locking is whether the compiler promises 
>> to generate the proper memory barriers such that other threads are 
>> guaranteed to see the assignment to _someDictionary *after* the object has 
>> been constructed. C makes no such guarantee; other threads might see a 
>> non-nil value for _someDictionary before the first thread is done 
>> constructing the object.
> 
> I'm fairly sure that @synchronized, being a compiler built-in, rather than a 
> function, makes that guarantee -- specifically the the values of instance 
> variables can change across entry and exit of an @synchronized block.

If you actually understood the problem with double-checked locking, you'd 
understand that the problem is with the early return that happens before the 
@synchronized block.

Please reread the Wikipedia article Ken linked to; the Java example exhibits 
*exactly* the same flaw as your Objective-C code.

> 
> Further, if writes were not complete at the end of the block, the construct 
> would be essentially useless for its intended purpose.

Again, the problem exists *before* entering the @synchronized block.


> 
> In any case, removing the outer check makes the code correct regardless of 
> compiler guarantees.

Yes, which is why your "optimization" is a classic anti-pattern.

--Kyle Sluder
_______________________________________________

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