Hi Dave,
thanks for replying. I must say some of what you said went above my head. :)
On Apr 14, 2011, at 2:02 PM, Dave Zarzycki wrote:
>> The first is that if the method gets invoked already in the queue's
>> automatic thread, there will be a deadlock. That's easy to fix, by wrapping
>> the dispatch call into a function that tests queue against the currently
>> executing queue and simply executes the block when they coincide.
>
> Actually, this isn't easy to fix due to X->A->B->A problems, where X is the
> current queue, then A, then B, and then the code deadlocks trying to
> dispatch_sync() against A "because it isn't the current queue".
Why isn't the following deadlock-free?
// In WLT_GCDUtils.m
+ (void) dispatchToQueue: (dispatch_queue_t) queue
async: (BOOL) async
block: (void (^)(void)) block;
{
if (dispatch_get_current_queue() != queue)
{
if (async)
{
dispatch_async(queue, block);
}
else
{
dispatch_sync(queue, block);
}
}
else // already in target queue
{
// Just in case we might need one...
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
block();
[pool release];
}
}
>> The second problem is that the pattern isn't actually thread safe. If two
>> threads (that aren't the automatic thread of the queue) enter the accessor,
>> two blocks will be enqueued serially. The two threads will then block,
>> waiting for their blocks to finish executing.
>>
>> So far so good but when the first block finishes, it could happen that the
>> first thread blocks until the second block finishes, at which time the foo
>> value computed by the first block will have been replaced by the value
>> computed by the second block, since foo is shared among all blocks that
>> captured it.
>>
>> Thus, when the first thread resumes, it will report the wrong foo value.
>
> Can you please provide a compilable test case?
I don't have an actual sample case where it has happened. Admittedly, this was
just a thought experiment.
Thanks again for your help.
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]