dispatch_sync does two things:

  1. queue a block
  2. 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:

  1. dispatch_sync queues the block in the main queue.
  2. dispatch_sync blocks the thread of the main queue until the block finishes executing.
  3. 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.

 https://stackoverflow.com/questions/10984732/why-cant-we-use-a-dispatch-sync-on-the-current-queue

dispatch_sync的更多相关文章

  1. GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验

    平常开发中会经常用gcd做一下多线程任务,但一直没有对同步.异步任务在串行.并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的. 代码如下: - (void)touchesB ...

  2. dispatch_sync may result in dead-lock

    以下代码会引起死锁 dispatch_block_t block = ^{ ; i < ; i++) { NSLog(@"dispatch_sync:%d", i); } } ...

  3. dispatch_async & dispatch_sync

    Clear that! dispatch_async 是将block发送到指定线程去执行,当前线程不会等待,会继续向下执行. dispatch_sync 也是将block发送到指定的线程去执行,但是当 ...

  4. 完整详细的说明GCD列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f

    为什么要写这个系列,由于百度了一下.我们正在寻找一个非常比较片面的Blog.抄来抄去,写作是很粗糙. 所以,我想写这个系列,尝试记录官方网站GCD强大的全功能的表达.为了方便他们,也方便他人,假设有发 ...

  5. dispatch_sync和dispatch_async的区别

    dispatch_sync 线程同步.dispatch_async线程异步 比如 //同步 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE ...

  6. ios同步线程(dispatch_sync)保证代码在主线程中执行

    - (BOOL)transitionToNextPhase { // 保证代码在主线程 if (![[NSThread currentThread] isMainThread]) { dispatch ...

  7. dispatch_async 和dispatch_sync

    dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...

  8. GCD学习(六) dispatch_async 和dispatch_sync

    dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...

  9. viewDidLoad dispatch_sync

    - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1"); dispatch_sync(dispatch_get_main_qu ...

  10. iOS Dispatch_sync 阻塞线程的原因

    大家的知道在主队列上使用dispatch_sync(), - (void)testSyncMainThread { dispatch_queue_t main = dispatch_get_main_ ...

随机推荐

  1. hdu_1036_Average is not Fast Enough_201311021335

    Average is not Fast Enough! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. [bzoj1935][Shoi2007]Tree 园丁的烦恼 _树状数组

    Tree 园丁的烦恼 bzoj-1935 Shoi-2007 题目大意:给定平面上的$n$个点,$m$次查询矩形点个数. 注释:$1\le n,m\le 5\cdot 10^5$. 想法:静态二维数点 ...

  3. hibernate分表保存日志

    @Service("accessLogService")@Transactionalpublic class LogMessageServiceImpl extends BaseD ...

  4. CCEditBox/CCEditBoxImplMac

    #ifndef __CCEditBoxIMPLMAC_H__ #define __CCEditBoxIMPLMAC_H__ #include "cocos2d.h" #if (CC ...

  5. 2015 测试赛 同构 hihoCoder

    题目1 : 同构 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定2个树A和B,保证A的节点个数>=B的节点个数. 现在你需要对树A的边进行二染色. 一个好的染色 ...

  6. HDU 4524

    简单题,先从右边消起,注意结束时a[1]==0才能是yes #include <iostream> #include <cstdio> #include <cstring ...

  7. 9.JAVA编程思想 多形性

    欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/51040241 "对于面向对象的程序设计语言,多型性是第三种最主要的特 ...

  8. The Elder HDU - 5956

    /* 树上斜率优化 一开始想的是构造出一个序列 转化成一般的dp但是可能被卡 扫把状的树的话可能变成n*n 其实可以直接在树上维护这个单调队列 dfs虽然搞得是一棵树,但是每次都是dfs到的都是一个序 ...

  9. CentOS 安装 MRTG 软件完成后的 403 Forbidden(转载)

    用 yum 安装 MRTG 並设定好之后也把 apache 的 httpd.conf 加上 mrtg 的目录,但 http://server/mrtg 卻一直出現 403 Forbidden.在 ht ...

  10. 2017 Multi-University Training Contest - Team 2 &hdu 6055 Regular polygon

    Regular polygon Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...