大家的知道在主队列上使用dispatch_sync(),

- (void)testSyncMainThread {

    dispatch_queue_t main = dispatch_get_main_queue();
NSLog(@"*********1");
dispatch_sync(main, ^(){
NSLog(@"*********2");
});
NSLog(@"*********3");
}

会造成线程阻塞,但是查找网上说的原因,发现基本上说法都是一样的,感觉不是不太好理解,

我查看一下API,

/*!

* @function dispatch_sync

*

* @abstract

* Submits a block for synchronous execution on a dispatch queue.

*

* @discussion

* Submits a workitem to a dispatch queue like dispatch_async(), however

* dispatch_sync() will not return until the workitem has finished.

*

* Work items submitted to a queue with dispatch_sync() do not observe certain

* queue attributes of that queue when invoked (such as autorelease frequency

* and QOS class).

*

* Calls to dispatch_sync() targeting the current queue will result

* in dead-lock. Use of dispatch_sync() is also subject to the same

* multi-party dead-lock problems that may result from the use of a mutex.

* Use of dispatch_async() is preferred.

*

* Unlike dispatch_async(), no retain is performed on the target queue. Because

* calls to this function are synchronous, the dispatch_sync() "borrows" the

* reference of the caller.

*

* As an optimization, dispatch_sync() invokes the workitem on the thread which

* submitted the workitem, except when the passed queue is the main queue or

* a queue targetting it (See dispatch_queue_main_t,

* dispatch_set_target_queue()).

*

* @param queue

* The target dispatch queue to which the block is submitted.

* The result of passing NULL in this parameter is undefined.

*

* @param block

* The block to be invoked on the target dispatch queue.

* The result of passing NULL in this parameter is undefined.

*/

结合API,我的理解:

第一步:当代码执行到 dispatch_sync()时,会首先把dispatch_sync()函数放到当前线程的队列(就是主队列中),而且这个线程并不会立即返回,而是会等待block中的任务都完成时才会返回,

第二步:再把block中的任务依次放到主队列中等待执行,

由于是同步执行,而且block中的任务在主队列的线性表中的前面有个dispatch_sync()等待完成,而dispatch_sync()的完成又依赖于block的执行完成,就这样造成了死锁。

最后:在其他队列上模拟这种死锁情况

 dispatch_queue_t lwMainQueue = dispatch_queue_create("lw.queue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(lwMainQueue, ^{
NSLog(@"*********1");
dispatch_sync(lwMainQueue, ^{NSLog(@"*********2");
});
NSLog(@"*********3");
});

效果是一样的

iOS Dispatch_sync 阻塞线程的原因的更多相关文章

  1. 使用runloop阻塞线程的正确写法

    使用runloop阻塞线程的正确写法 runloop可以阻塞线程,等待其他线程执行后再执行. 比如: @implementation ViewController{    BOOL end;}…– ( ...

  2. iOS 多线程之线程锁Swift-Demo示例总结

    线程锁是什么 在前面的文章中总结过多线程,总结了多线程之后,线程锁也是必须要好好总结的东西,这篇文章构思的时候可能写的东西得许多,只能挤时间一点点的慢慢的总结了,知道了线程之后要了解线程锁就得先了解一 ...

  3. tornado 异步调用系统命令和非阻塞线程池

    项目中异步调用 ping 和 nmap 实现对目标 ip 和所在网关的探测 Subprocess.STREAM 不用担心进程返回数据过大造成的死锁, Subprocess.PIPE 会有这个问题. i ...

  4. fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因

    很开心的宣布 Log Guru 正式开源! Log Guru,是 fir.im 开发团队创造的小轮子,用在 Mac 电脑上的日志获取,Github 地址:FIRHQ/LogGuru. Log Guru ...

  5. iOS中的线程安全问题

    为了保证线程安全,不会因为多个线程访问造成资源抢夺,出现的运行结果的偏差问题,我们需要使用到线程同步技术,最常用的就是 @synchronized互斥锁(同步锁).NSLock.dispatch_se ...

  6. Delphi Socket 阻塞线程下为什么不触发OnRead和OnWrite事件

    //**********************************************************************************//说明: 阻塞线程下为什么不触 ...

  7. delphi TServerSocket阻塞线程单元 实例

    TServerSocket阻塞线程单元,希望对你有所帮助.需要注意的是:1.如果你使用TServerSocket的stNonBlocking模式,重写TServerClientThread线程时要重载 ...

  8. iOS中保证线程安全的几种方式与性能对比

    来源:景铭巴巴 链接:http://www.jianshu.com/p/938d68ed832c 一.前言 前段时间看了几个开源项目,发现他们保持线程同步的方式各不相同,有@synchronized. ...

  9. IOS 多线程,线程同步的三种方式

    本文主要是讲述 IOS 多线程,线程同步的三种方式,更多IOS技术知识,请登陆疯狂软件教育官网. 一般情况下我们使用线程,在多个线程共同访问同一块资源.为保护线程资源的安全和线程访问的正确性. 在IO ...

随机推荐

  1. thinkphp5.1常量定义使用

    thinkphp5.1取消了系统常量 可以把常量配置在app.php文件中 //配置网站地址 'WEB_URL'=>'http://127.0.0.1/tp5', 可以使用config()函数直 ...

  2. 安装php过程中的错误和解决方式 configure: error: jpeglib.h not found

    centos6.5 32位系统: checking for the location of libpng... yeschecking for the location of libXpm... no ...

  3. 什么是obj文件?

    百度百科: 程序编译时生成的中间代码文件.目标文件,一般是程序编译后的二进制文件,再通过链接器(LINK.EXE)和资源文件链接就成可执行文件了.OBJ只给出了程序的相对地址,而可执行文件是绝对地址. ...

  4. Luogu P3265 [JLOI2015]装备购买

    好吧刚开始不知道自己在写什么,,,后来写了线性方程组,又过了一天一上午终于明白了... 当然题意很显然:求代价最小的极大线性无关组. 那就高斯消元(好吧刚开始我不会用它来解这道题qwq) 第一个循环是 ...

  5. 洛谷P3195||bzoj1010 [HNOI2008]玩具装箱TOY

    洛谷P3195 bzoj1010 设s数组为C的前缀和 首先$ans_i=min_{j<i}\{ans_j+(i-j-1+s_i-s_j-L)^2\}$ (斜率优化dp)参考(复读)https: ...

  6. Caused by: MetaException(message:Hive Schema version 2.1.0 does not match metastore's schema version 1.2.0 Metastore is not upgraded or corrupt)_2

    Caused by: MetaException(message:Hive Schema version 2.1.0 does not match metastore's schema version ...

  7. 自定义滚动条插件 mCustomScrollbar 使用介绍

    引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...

  8. ip route,ip rule, iptables和docker的端口映射

    iptables 默认5个表, 不可增加其他表 raw 用于配置数据包,raw 中的数据包不会被系统跟踪. filter 是用于存放所有与防火墙相关操作的默认表. nat 用于 网络地址转换(例如:端 ...

  9. TCP/IP三次握手与四次挥手的正确姿势

    0.史上最容易理解的:TCP三次握手,四次挥手 https://cloud.tencent.com/developer/news/257281 A 理解TCP/IP三次握手与四次挥手的正确姿势http ...

  10. CentOS Linux解决Device eth0 does not seem to be present【转】

    在VMware里克隆出来的CentOS Linux,ifconfig...没有看到eth0,然后重启网卡又报下面错误. 故障现象: service network restartShutting do ...