iOS Dispatch_sync 阻塞线程的原因
大家的知道在主队列上使用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 阻塞线程的原因的更多相关文章
- 使用runloop阻塞线程的正确写法
使用runloop阻塞线程的正确写法 runloop可以阻塞线程,等待其他线程执行后再执行. 比如: @implementation ViewController{ BOOL end;}…– ( ...
- iOS 多线程之线程锁Swift-Demo示例总结
线程锁是什么 在前面的文章中总结过多线程,总结了多线程之后,线程锁也是必须要好好总结的东西,这篇文章构思的时候可能写的东西得许多,只能挤时间一点点的慢慢的总结了,知道了线程之后要了解线程锁就得先了解一 ...
- tornado 异步调用系统命令和非阻塞线程池
项目中异步调用 ping 和 nmap 实现对目标 ip 和所在网关的探测 Subprocess.STREAM 不用担心进程返回数据过大造成的死锁, Subprocess.PIPE 会有这个问题. i ...
- fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因
很开心的宣布 Log Guru 正式开源! Log Guru,是 fir.im 开发团队创造的小轮子,用在 Mac 电脑上的日志获取,Github 地址:FIRHQ/LogGuru. Log Guru ...
- iOS中的线程安全问题
为了保证线程安全,不会因为多个线程访问造成资源抢夺,出现的运行结果的偏差问题,我们需要使用到线程同步技术,最常用的就是 @synchronized互斥锁(同步锁).NSLock.dispatch_se ...
- Delphi Socket 阻塞线程下为什么不触发OnRead和OnWrite事件
//**********************************************************************************//说明: 阻塞线程下为什么不触 ...
- delphi TServerSocket阻塞线程单元 实例
TServerSocket阻塞线程单元,希望对你有所帮助.需要注意的是:1.如果你使用TServerSocket的stNonBlocking模式,重写TServerClientThread线程时要重载 ...
- iOS中保证线程安全的几种方式与性能对比
来源:景铭巴巴 链接:http://www.jianshu.com/p/938d68ed832c 一.前言 前段时间看了几个开源项目,发现他们保持线程同步的方式各不相同,有@synchronized. ...
- IOS 多线程,线程同步的三种方式
本文主要是讲述 IOS 多线程,线程同步的三种方式,更多IOS技术知识,请登陆疯狂软件教育官网. 一般情况下我们使用线程,在多个线程共同访问同一块资源.为保护线程资源的安全和线程访问的正确性. 在IO ...
随机推荐
- java IO流部分知识点
IO流部分 IO流常用的有:字符流.字节流.缓冲流.序列化流.RandomAccessFile类等 1.字节流 FileInputStream/FileOutputStream BufferedInp ...
- 洛谷P3193 [HNOI2008]GT考试(KMP,矩阵)
传送门 大佬讲的真吼->这里 首先考虑dp,设$f[i][j]$表示长串匹配到第$i$位,短串最多匹配到$j$位时的方案数 那么答案就是$\sum_{i=0}^{m-1}f[n][i]$ 然后考 ...
- xml 的使用和解析 及解析工具
xml 一.xml简介 1. 什么是xml XML:Extensiable Markup Language,可扩展标记语言.和HTML有语法相似之处,也有作用上的不同: 和html相似: 都是由一堆标 ...
- AT2301 Solitaire
传送门 这里提供智障的\(O(n^2)\)做法 其实是有\(O(logn)\)做法的,但是我太菜了想不出来 Solution: 首先可以发现生成的序列一定是一个两边向中间单调递减的序列 这样就可以发现 ...
- MacOS下,Python2和Python3完美兼容使用(转)
问题阐述: MacOS默认Python版本是2.7.10,随着Python3的进一步占有市场,Python2.7也将在2020年结束维护,所以在同一台电脑上安装多个Python版本势在必行. 一.py ...
- Ext3.1的一些使用讨论
这里简单记录一下曾经的10个月使用Ext的工作模式. 前公司用的是 Ext 3.1,在2018/2019的今天,可以说是比较久远的技术了.处于大前端发展时代的我们,对其的诟病应该不少. 不过其中面向对 ...
- Ubuntu下rsyslog集中收集mysql审计日志
服务端 1.安装最新版本rsyslog sudo apt-get install software-properties-common python-software-properties sudo ...
- Unity 行为树-共享变量
一.引言 有以下小场景: 节点A:发现了 敌人. 节点B:追逐敌人. 对于同一个敌人物体,节点AB之间是如何传递数据 的呢? 行为树节点AB之间,需要一个中间变量Temp来传递数据. A发现了敌人,将 ...
- 自增长 auto_increment
auto_increment :自动编号,一般与主键组合使用.一个表里面只有一个自增默认情况下,起始值为1,每次的增量为1. 例子:create table tb5( id int primar ...
- Sharepoint JSCOM 列表操作
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItemsInclude); //确保js文件加载,再执行方法 function ...