About calling of dispatch_queue_t and dispatch_sync

冷暖自知 提交于 2019-12-12 04:33:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!