On Apr 14, 2011, at 1:19 PM, Jonathan Taylor wrote:

> I tested this out before replying as I wasn't 100% certain. It may be that we 
> have misunderstood each other somehow, but the following code (in a clean new 
> project) was what I used to confirm to myself that two concurrently-executing 
> threads have independent instances of the variable:
> http://www.dur.ac.uk/j.m.taylor/block_test_code.m

More likely, I've misunderstood the documentation. It's happened before, many 
times. :)

I looked at your code only briefly now but will return to study it in detail 
later (kinda busy right now). I must say that I'm confused now more than 
before, because I really thought that the __block qualifier meant that there is 
only one instance of that variable, shared by all scopes that declare it and 
shared by all blocks that capture it.


>>> However I don't think you should need to do this anyway. I would change 
>>> your code to something like this:
>>> 
>>> - (SomeObjType) foo
>>> {
>>> dispatch_sync(queue,
>>> ^{
>>>     // Code in this block ensures bah is valid
>>>     if (nil == bah)
>>>     {
>>>         // Code to compute and store bah goes here
>>>     }
>>> });
>>> 
>>> return bah;
>>> }
>> 
>> I don't see how that could possibly work as is because bah is out of scope 
>> by the time the return statement is executed. Perhaps you meant that bah is 
>> an ivar or property declared in the same class as the accessor. I'm suddenly 
>> drawing a blank here because I considered this before and came to the 
>> conclusion that it wouldn't work but now I can't remember why I thought that 
>> way. I need to think this through a bit more.

> OK, well since you do not declare bah locally, and from what you are doing it 
> is presumably meant to be persistent, I assumed that it was indeed a property 
> (or a global?). If you are doing something else with it (though I'm not quite 
> sure what...) then obviously what I wrote may be wrong...

No, actually you are totally correct here. As it happens, I do use the pattern 
you suggest, in several places where the conditions are precisely those you 
laid out: the ivars are meant to be instantiated only once and never released. 
It's in other cases where those conditions break that I'm struggling with.

Clearly I need to refine my understanding of blocks and discussions like this 
greatly help.

Thanks again.
WT_______________________________________________

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