关于调用dispatch_queue_t和dispatch_sync [英] About calling of dispatch_queue_t and dispatch_sync

查看:112
本文介绍了关于调用dispatch_queue_t和dispatch_sync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要代码if (dispatch_get_current_queue() == socketQueue)?为什么我们不能直接使用dispatch_sync(socketQueue, block) ???

Why the code if (dispatch_get_current_queue() == socketQueue) is needed? why can't we just use dispatch_sync(socketQueue, block) directly???

提前谢谢!

- (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;
}

顺便说一句,代码来自 XMPPFramework

BTW, the code is from XMPPFramework

推荐答案

您无法调用dispatch_sync来调度当前串行队列上的块,因为这将导致死锁. Dispatch_sync会等到该块完成执行,但是它永远不会在当前块完成运行之前开始运行.

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.

这篇关于关于调用dispatch_queue_t和dispatch_sync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆