dispatch_sync
dispatch_sync
does two things:
- queue a block
- blocks the current thread until the block has finished running
Given that the main thread is a serial queue (which means it uses only one thread), the following statement:
will cause the following events:
dispatch_sync
queues the block in the main queue.dispatch_sync
blocks the thread of the main queue until the block finishes executing.dispatch_sync
waits forever because the thread where the block is supposed to run is blocked.
The key to understanding this is that dispatch_sync
does not execute blocks, it only queues them. Execution will happen on a future iteration of the run loop.
The following approach:
I know where your confusion comes from:
As an optimization, this function invokes the block on the current thread when possible.
Careful, it says current thread.
Thread != Queue
A queue doesn't own a thread and a thread is not bound to a queue.
I found this in the documentation (last chapter):
Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.
Also, I followed the link that you provided and in the description of dispatch_sync I read this:
Calling this function and targeting the current queue results in deadlock.
So I don't think it's a problem with GCD, I think the only sensible approach is the one you invented after discovering the problem.
dispatch_sync的更多相关文章
- GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验
平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...
- dispatch_sync may result in dead-lock
以下代码会引起死锁 dispatch_block_t block = ^{ ; i < ; i++) { NSLog(@"dispatch_sync:%d", i); } } ...
- dispatch_async & dispatch_sync
Clear that! dispatch_async 是将block发送到指定线程去执行,当前线程不会等待,会继续向下执行. dispatch_sync 也是将block发送到指定的线程去执行,但是当 ...
- 完整详细的说明GCD列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
为什么要写这个系列,由于百度了一下.我们正在寻找一个非常比较片面的Blog.抄来抄去,写作是很粗糙. 所以,我想写这个系列,尝试记录官方网站GCD强大的全功能的表达.为了方便他们,也方便他人,假设有发 ...
- dispatch_sync和dispatch_async的区别
dispatch_sync 线程同步.dispatch_async线程异步 比如 //同步 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE ...
- ios同步线程(dispatch_sync)保证代码在主线程中执行
- (BOOL)transitionToNextPhase { // 保证代码在主线程 if (![[NSThread currentThread] isMainThread]) { dispatch ...
- dispatch_async 和dispatch_sync
dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...
- GCD学习(六) dispatch_async 和dispatch_sync
dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...
- viewDidLoad dispatch_sync
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1"); dispatch_sync(dispatch_get_main_qu ...
- iOS Dispatch_sync 阻塞线程的原因
大家的知道在主队列上使用dispatch_sync(), - (void)testSyncMainThread { dispatch_queue_t main = dispatch_get_main_ ...
随机推荐
- (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】
在读此文章之前您还可能需要先了解: (39.1) Spring Boot Shiro权限管理[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/b ...
- N天学习一个Linux命令之hostnamectl
前言 安装了CentOS7,发现按照以前修改文件/etc/sysconfig/network HOSTNAME字段主机名的方式不生效了,查资料发现可以使用hostnamectl命令 用途 Contro ...
- ZOJ 3213
/* ZOJ 3213 好吧,看过那种括号表示法后,就崩溃了,实在受不了.情况复杂,写了两天,人也有点傻X了,只能放弃,转而用最小表示法. 最小表示法不难写: 1)首先,要承认路径上有格子不选的情况, ...
- ORACLE11G设置IP訪问限制
出于数据安全考虑,对oracle数据库的IP做一些限制,仅仅有固定的IP才干訪问. 改动$JAVA_HOME/NETWORK/ADMIN/sqlnet.ora文件 添加下面内容(红色表示凝视): #开 ...
- CF 557A(Ilya and Diplomas-贪心)
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...
- jQuery总结04
1 JavaScript 中的 AJAX 的四个实现步骤分别是? 2 如何处理 XMLHttpRequest 对象的兼容问题? 3 jQuery 中的 AJAX 4 jQuery 选择器包括哪些? 5 ...
- 使用ALSA编写自己的音频程序【转】
本文转载自:http://blog.csdn.net/beyondioi/article/details/6994548 Alsa是Linux高级音频接口.面对众多的音频设备,Alsa为Linux音频 ...
- hdu 3037Saving Beans(卢卡斯定理)
Saving Beans Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- codevs1253 超级市场(dp)
1253 超级市场 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 某人喜欢按照自己的规则去市场买菜,他每天 ...
- MYSQL 数据库命令行终端操作笔记
1.数据库登录: 1.登录本地的MYSQL数据库:mysql -u root -p 2.连接远程主机上的MYSQL数据库:mysql -h 192.168.191.2 -u root -p 123 ...