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_syncqueues the block in the main queue.dispatch_syncblocks the thread of the main queue until the block finishes executing.dispatch_syncwaits 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_ ...
随机推荐
- Python 4 循环语句while
while [条件]: 条件这里满足布尔运算True则无限循环while里面代码. 固定条件的 基本的while循环, 如果if匹配那么 则执行打印登录成功,和break跳出整个循环, ...
- fork me on github 彩带设置无效
挑选彩带地址: https://github.com/blog/273-github-ribbons 发现代码复制粘贴过来,但是在自己博客园上无效,如粘贴如下代码 <a href="h ...
- 从事分布式系统,计算,hadoop
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
- <script>标签的加载解析执行
转自原文 <script>标签的加载解析执行 看了很多网上的文章,都是大同小异.总结一下.内部原理还没有搞清楚,有机会再学习. 一.<script>标签的加载解析执行顺序 ht ...
- Python开发工具安装
v阅读目录 v写在前面 v基本概念 vWindows搭建python开发环境 v从Hello World开始 v博客总结 v博客前言 从大学开始玩python到现在参加工作,已经有5年了,现在的公司是 ...
- C语言编程入门——程序练习(上)
大家能够敲写一下以下的练习代码.看下执行结果,都非常easy.关键要理解. if: # include <stdio.h> int main(void) { int i = 1; i = ...
- 【译文】利用STAN做贝叶斯回归分析:Part 2 非正态回归
[译文]利用STAN做贝叶斯回归分析:Part 2 非正态回归 作者 Lionel Hertzogn 前一篇文章已经介绍了怎样在R中调用STAN对正态数据进行贝叶斯回归.本文则将利用三个样例来演示怎样 ...
- 转:java身份证格式强校验
package com.dsh.zealandweb.utils; import java.util.HashSet; import java.util.regex.Pattern; import o ...
- poj2342 Anniversary party (树形dp)
poj2342 Anniversary party (树形dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9128 ...
- boost库生成文件命名和编译
生成文件命名规则:boost中有许多库,有的库需要编译.而有的库不需要编译,只需包含头文件就可以使用.编译生成的文件名字普遍较长,同一个库根据编译链接选项不同,又可以生成多个不同名字的文件.生成的文件 ...