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_ ...
随机推荐
- 【郑轻邀请赛 D】hipercijevi
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 [题意] [题解] 把那个管泛化成一个点; 然后把每一个在管里面的点都和它相连 ...
- [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]
根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...
- [网络流24题#9] [cogs734] 方格取数 [网络流,最大流最小割]
将网格分为两部分,方法是黑白染色,即判断(i+j)&1即可,分开后从白色格子向黑色格子连边,每个点需要四条(边界点可能更少),也就是每个格子周围的四个方向.之后将源点和汇点分别于黑白格子连边, ...
- POJ 3304 segments 线段和直线相交
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14178 Accepted: 4521 Descrip ...
- 关于卷积网络以及反卷积网络shape的计算
CNN的计算方式: w1 = (w - F_w + 2p) / s_w + 1 h1 = (h - F_h + 2p) / s_h + 1 其中 w, h 分别为上一层的宽高, Filters(ker ...
- codeforces 437D The Child and Zoo
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- selenium IDE 回放报错
解决:Selenium RC未启动,启动即可. java -jar selenium-server-standalone-2.25.0.jar 启动RC报错,提示找不到firefox的path,于是配 ...
- "Hello World " —— 深入理解程序从编译到执行
对于C语言编写的Hello World程序(例如以下).对于程序猿来说肯定如雷贯耳,就是这样一个简单的程序,你真的了解她吗? #include <stdio.h> int main() { ...
- hash哈希
我复习的时候,突然发现没写过hash算法,惊讶!!!赶紧补一下. 把字符串看成base进制的数.Hash值比较就是为了判断是否有相同的字符串.(base是自己定义的大于26的质数,个人认为大一点比较好 ...
- 洛谷 P2668 & P2540 [ noip 2015 ] 斗地主 —— 搜索+贪心
题目:https://www.luogu.org/problemnew/show/P2668 https://www.luogu.org/problemnew/show/P2540 首先,如果没有 ...