由于iOS7以后 dispatch_get_current_queue 被废弃,所以需要寻找一个替代的方案。

发现 dispatch_get_current_queue 并没有字面上那么简单。

这个函数一般都会跟 dispatch_async 等API配合,

但是试想一下,我们自己创建的线程(比如 NSThread)跟 dispatch_queue_t 没有关系,

那么在我们的线程中调用 dispatch_get_current_queue 会返回什么呢?

     [NSThread detachNewThreadSelector:@selector(onPlay:) toTarget:self withObject:nil];

     - (void)onPlay
{
dispatch_queue_t dispatch_queue = dispatch_get_current_queue(); // 这里会返回什么?
}

在上面的代码中,我们并没有将onPlay显示投递到某一个dispatch_queue中,

但是dispatch_get_current_queue还是会返回一个dispatch_queue,

一个名字叫 com.apple.root.default-overcommit-priority 的 dispatch_queue!

但是!

如果在 onPlay 中打断点,你会发现,onPlay 并不在 com.apple.root.default-overcommit-priority 这个队列中!

     [NSThread detachNewThreadSelector:@selector(onPlay:) toTarget:self withObject:nil];

     - (void)onPlay
{
NSLog(@"1");
dispatch_async(dispatch_get_current_queue(), ^{
NSLog(@"2");
});
}

两句NSLog将不在同一个queue中打印!

好,以下分析dispatch_get_current_queue更深一层的行为。

先分析官方文档的说法:

函数声明:

dispatch_queue_t dispatch_get_current_queue(void);

描述(description):

Returns the queue on which the currently executing block is running.

返回当前所在的队列(dispatch_queue_t)。

详述(discussion):

This function is defined to never return NULL.

When called from outside of the context of a submitted block,

this function returns the main queue if the call is executed from the main thread.

If the call is made from any other thread, this function returns the default concurrent queue.

当调用这个函数的地方不是在某个已提交到队列的block内部时,

如果在主线程中被调用,则返回main_queue。

如果不在主线程中调用,则返回一个默认的并行队列。

我们先尝试将线程分成三种类型:

主线程

只有一个,并且存在一个与该线程绑定的 dispatch_queue,名字是 com.apple.main-thread,可以通过 dispatch_get_main_queue 得到。

dispatch_queue 线程

dispatch_queue 自己创建的线程,该线程当然是与创建它的 dispatch_queue 绑定。

其他线程

通过NSThread,POSIX,[NSObject performSelectorInBackground]等接口创建出来的线程,没有与之绑定的 dispatch_queue。

那么官方文档说的其他线程(any other thread),指的就是除主线程,dispatch_queue 线程之外的线程。

这些线程没有与之绑定的 dispatch_queue,所以会返回一个 “默认的并行队列(default concurrent queue)”。

这是个什么东西呢?我们用文章开头的代码,打印出这个队列的名字。

     [NSThread detachNewThreadSelector:@selector(onPlay:) toTarget:self withObject:nil];

     - (void)onPlay
{
dispatch_queue_t dispatch_queue = dispatch_get_current_queue();
NSLog(@"%s", dispatch_queue_get_label(dispatch_queue));
}

其结果是:com.apple.root.default-overcommit-priority。

也就是说,如果执行 dispatch_get_current_queue 的线程没有与之绑定的队列的时候,

会返回一个 com.apple.root.default-overcommit-priority 的队列,这个队列跟该线程没有关系。

可以理解为 dispatch_get_current_queue 这个函数的例外情况。

可以猜测 dispatch_get_current_queue 被废弃的原因就是这个函数不一定总是有意义的。

每一个well-known global queue 都有一个与之对应的 overcommit 类型的queue。

overcommit 类型的 queue 具体是什么用途,为什么会分开暂时还不知道。

整理:

以下是iOS应用程序中默认创建的队列:

优先级                                             名称

well-known global queue

High priority                         (com.apple.root.high-priority)

Default priority                     (com.apple.root.default-priority)

Low priority                          (com.apple.root.low-priority)

Background priority              (com.apple.root.background-priority)

overcommit global queue

High priority                          (com.apple.root.high-overcommit-priority)

Default priority                      (com.apple.root.default-overcommit-priority)

Low priority                           (com.apple.root.low-overcommit-priority)

Background priority               (com.apple.root.background-overcommit-priority)

main queue

Main-Thread                          (com.apple.main-thread)

各种API获取到的队列:

dispatch_get_global_queue                              相应优先级的 well-known global queue

dispatch_get_main_queue                                com.apple.main-thread

各种情况下调用dispatch_get_current_queue得到的队列:

主线程中                                                        com.apple.main-thread

NSThread                                                      com.apple.root.default-overcommit-priority

pthread                                                          com.apple.root.default-overcommit-priority

[NSObject performSelectorInBackground]     com.apple.root.default-overcommit-priority

NSOperationQueue                                       com.apple.root.default-priority

NSOperationQueue使用GCD实现,具体内部会使用 com.apple.root.default-priority 队列来执行任务。

NSBlockOperation或者NSInvocationOperation都会被投递到这个dispatch_queue中。

dispatch_get_current_queue 废弃的更多相关文章

  1. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  2. mysql_connect() php7不支持,php5.5可以,是废弃函数

    天用了PHP7,发现和PHP5变化还挺大的,最大的就是MySQL的连接库变了. PHP5中使用mysql_connect()函数进行连接,但实际上,PHP5.5开始,MySQL就不推荐使用了,属于废弃 ...

  3. 让我们一起用开源数据库和开源框架废弃Access

    一.为什么要废弃Access? 1.客户的机子上需要安装access的驱动 ps:这个比较烦人,大家都知道部署越简单越好,安装这个对用户来说太繁琐了. 2.操作时性能不佳 using System; ...

  4. PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)

    1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...

  5. svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录

    svn更新路径,解决办法详细步骤,eclipse里面的更新方法,svn废弃位置,Windows环境,svn服务器地址换了,如何更新本地工作目录 Windows下,svn服务器IP本来是内网一台服务器上 ...

  6. 关于iOS和OS X废弃的API你需要知道的一切

    如你所知,已废弃(Deprecated)的API指的是那些已经过时的并且在将来某个时间最终会被移除掉的方法或类.通常,苹果在引入一个更优秀的API后就会把原来的API给废弃掉.因为,新引入的API通常 ...

  7. [译]关于iOS和OS X废弃的API你需要知道的一切

    原文: Everything You Need to Know about iOS and OS X Deprecated APIs 如你所知,已废弃(Deprecated)的API指的是那些已经过时 ...

  8. HTML5之废弃和更新的元素与属性

    废弃的元素和属性 [1]标签替换 <acronym> 替代:<abbr> <applet> 替代:<embed> 或 <object> &l ...

  9. 关于iOS和OS X废弃的API知识点

    今天在查看苹果接口文档时,突然对于接口的声明知识点比较感兴趣,再网络找到下面这个比较不错的文章,记录一下并分享: 如你所知,已废弃(Deprecated)的API指的是那些已经过时的并且在将来某个时间 ...

随机推荐

  1. [转] CSS direction属性简介与实际应用 ---张鑫旭

    一.用的少并不代表没有用 至少,在我接触的这么多项目里,没有见到使用过CSS direction属性做实际开发的. 为什么呢?是因为direction长得丑吗? 虽然说direction确实其貌不扬, ...

  2. js 默认的参数、可变的参数、变量作用域

    可以通过arguments对象来实现可变的参数 在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. arguments是一个数组对象,可以通过下标来实别参数的位置 ...

  3. 关于 Log4Net

    Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过日志查看系统的运行过程,从而发现系统的问题.日志的作用:将运 ...

  4. xfire实现webservice客户端之测试关注点

    日前的工作接触到很多系统间的Webservice调用,这里想谈谈基于spring+xfire实现的webservice的客户端踩过的一些坑,需要测试关注的点. xFire的配置项 在spring中实现 ...

  5. iOS:使用导航栏

    要求使用ARC // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. ...

  6. [HDOJ 5183] Negative and Positive (NP) 【Hash】

    题目链接:HDOJ - 5183 题目分析 分两种情况,奇数位正偶数位负或者相反. 从1到n枚举,在Hash表中查询 Sum[i] - k ,然后将 Sum[i] 加入 Hash 表中. BestCo ...

  7. 创新高性能移动 UI 框架-Canvas UI 框架

    WebView 里无法获得的能力虽然是「体验增强」与「端基本能力」,但现都基本上有成熟解决方法.但后期的 UI 和 Layout 的性能反而是目前 Web 技术欠缺的.所以,无论是 Titanium ...

  8. NWERC 2012 Problem E Edge Case

    比赛的时候刷了一点小聪明,发现这个数列是卢卡斯数,一个递推关系像斐波拉契数列的数列: 我不知道怎么证明,如果哪天无意中会证了再加上: 这题唯一的难点就是大数运算: 直接用JAVA 代码: import ...

  9. 通过 DevOps 整合开发和应用安全管道

    [编者按]作者 Aaron Volkmann 是 CERT Division 高级研究员,通过提出了一种集成安全系统到 CI/CD 的方法,让机构保持快速部署到生产环境能力的同时,也大幅度降低安全隐患 ...

  10. 大数据计算新贵Spark在腾讯雅虎优酷成功应用解析

    http://www.csdn.net/article/2014-06-05/2820089 摘要:MapReduce在实时查询和迭代计算上仍有较大的不足,目前,Spark由于其可伸缩.基于内存计算等 ...