问题
Why the code if (dispatch_get_current_queue() == socketQueue)
is needed? why can't we just use dispatch_sync(socketQueue, block)
directly???
Thanks in advance!
- (BOOL)isConnected
{
__block BOOL result = NO;
dispatch_block_t block = ^{
result = (flags & kConnected) ? YES : NO;
};
if (dispatch_get_current_queue() == socketQueue)
block();
else
dispatch_sync(socketQueue, block);
return result;
}
BTW, the code is from XMPPFramework
回答1:
You cannot call dispatch_sync
to schedule blocks on the current serial queue since this will deadlock. Dispatch_sync waits until the block finished executing, but it will never start to run before the current block finished running.
来源:https://stackoverflow.com/questions/11305450/about-calling-of-dispatch-queue-t-and-dispatch-sync