想了许久,还是决定将GCD的内容尽量写得简单一点,所以多分几个章节,每个章节内容尽量少。
一、如何创建一个队列
GCD提供了3个创建队列的API,下面就通过这是三个API来看看队列的优先级中的那些事。
1.1 dispatch_get_main_queue()
- 测试代码:
dispatch_queue_t mainQueue = dispatch_get_main_queue();
接受参数:
void
输出结果1:
po mainQueue
(lldb) po mainQueue
<OS_dispatch_queue_main: com.apple.main-thread[0x1003364c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = com.apple.root.default-qos.overcommit[0x100336940], width = 0x1, state = 0x001ffe1000000304, in-flight = 0, thread = 0x307 }>
-
OS_dispatch_queue_main: com.apple.main-thread[0x1003364c0]
:主队列队列类型:OS_dispatch_queue_main
,标签:com.apple.main-thread
,[0x1003364c0]
:队列的地址; -
xref/ref/sref = 1
:ref一般表示引用,只要是对象,都满足ARC里面的引用计数规则,所以这三个属性应该是表示某种引用计数。但这个1
表示的是自己的引用计数,还是target的引用计数还无法确定,答案可能藏在后面的分析中; -
target = com.apple.root.default-qos.overcommit[0x100336940]
:让人耳目一新的属性出来的,主队列仍然是依赖于某个队列,这个队列又有啥特点呢? -
width = 0x1
:这个简单,串行队列,宽度为1,表示任务只能逐个执行; -
state = 0x001ffe1000000304
:是一个地址,因为对内存地址的取值不太清楚😣,所以这里也无法确定是一个对象地址,还是表示一个值,希望后面的分析中还能看到; -
in-flight = 0
:第一次见,这里也不做展开分析,重点是队列的优先级; -
thread = 0x307
:众所周知,系统会给主队列分配一个主线程;
- 输出结果2:
(lldb) po 0x100336940
<OS_dispatch_queue_global: com.apple.root.default-qos.overcommit[0x100336940] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
-
OS_dispatch_queue_global
:主线程所依赖的队列是一个全局队列,有点意思😁; -
target
:target为空,表示已经没有所依赖的队列了;
1.2 dispatch_get_global_queue()
-
测试代码:
// 全局队列(默认优先级) dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); NSLog(@"globalQueue : %@", globalQueue); // 全局队列(高优先级) dispatch_queue_t globalQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); NSLog(@"globalQueueHigh : %@", globalQueueHigh); // 全局队列(低优先级) dispatch_queue_t globalQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); NSLog(@"globalQueueLow : %@", globalQueueLow);
-
接受参数:
-
intptr_t identifier
:队列优先级,只接受下面四个值:
- DISPATCH_QUEUE_PRIORITY_HIGH: QOS_CLASS_USER_INITIATED - DISPATCH_QUEUE_PRIORITY_DEFAULT: QOS_CLASS_DEFAULT - DISPATCH_QUEUE_PRIORITY_LOW: QOS_CLASS_UTILITY - DISPATCH_QUEUE_PRIORITY_BACKGROUND: QOS_CLASS_BACKGROUND
-
uintptr_t flags
:一个预留参数,目前只接受0,其他任何值都会返回NULL;
-
输出结果:
(lldb) po globalQueue
<OS_dispatch_queue_global: com.apple.root.default-qos[0x1003368c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po globalQueueHigh
<OS_dispatch_queue_global: com.apple.root.user-initiated-qos[0x1003369c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po globalQueueLow
<OS_dispatch_queue_global: com.apple.root.utility-qos[0x1003367c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
- 这是三个不同的队列,而且地址是相邻的,间距128字节,可能是队列size,;
- 这三个队列都是
OS_dispatch_queue_global
类型的队列,而且target都为空,有可能是根队列。主队列所依赖的队列地址为:0x100336940
,与0x1003367c0
中间还有两个间距,将它们打印出来瞅瞅;
- 输出结果2:
po 0x1003365c0
<OS_dispatch_queue_global: com.apple.root.maintenance-qos[0x1003365c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x1003366c0
<OS_dispatch_queue_global: com.apple.root.background-qos[0x1003366c0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
- 结果不负众望,确实有两个队列,而且队列标签还不尽相同,分别是:
com.apple.root.maintenance-qos
、com.apple.root.background-qos
; - 既然有规律,那就按照这个规律打印更多的地址试试看;
- 输出结果3:
po 0x100336Ac0
<OS_dispatch_queue_global: com.apple.root.user-interactive-qos[0x100336ac0] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336Bc0
4298337216
(lldb) po 0x100336Cc0
4298337472
(lldb) po 0x100336Dc0
4298337728
(lldb) po 0x100336Ec0
4298337984
(lldb) po 0x100336Fc0
4298338240
- 很可惜,只找到了地址:
0x100336ac0
所对应的队列,听说有12种,但目前只看到了6种,会不会是我们创建的队列不够多,而根队列的加载方式属于懒加载,所以才无法找到其他的几条队列呢? - 这个问题就放在第二轮,创建更多类型的队列之后再验证。
1.3 dispatch_queue_create()
前面两个方法都是获取系统给我们提供的队列,GCD也提供了API给我们自己创建队列,上代码。
- 测试代码:
// 并行队列(默认优先级)
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.xy.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"concurrentQueue : %@", concurrentQueue);
// 串行队列(默认优先级)
dispatch_queue_t serialQueue = dispatch_queue_create("com.xy.serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"serialQueue : %@", serialQueue);
-
接受参数:
-
label
:队列的标签; -
dispatch_queue_attr_t attr
:可以有两种值:- 预定义好的属性:
DISPATCH_QUEUE_SERIAL
、DISPATCH_QUEUE_CONCURRENT
,分别表示串行和并行队列; - 通过
dispatch_queue_attr_make_with_
创建特定的属性
- 预定义好的属性:
-
输出结果:
(lldb) po serialQueue
<OS_dispatch_queue_serial: com.xy.serialQueue[0x100504490] = { xref = 1, ref = 1, sref = 1, target = com.apple.root.default-qos.overcommit[0x100336940], width = 0x1, state = 0x001ffe2000000000, in-flight = 0}>
(lldb) po concurrentQueue
<OS_dispatch_queue_concurrent: com.xy.concurrentQueue[0x100504200] = { xref = 1, ref = 1, sref = 1, target = com.apple.root.default-qos[0x1003368c0], width = 0xffe, state = 0x0000041000000000, in-flight = 0}>
- 串行队列
serialQueue
的目标队列0x100336940
,跟主队列的目标队列是同一个; - 并行队列
concurrentQueue
的目标队列0x1003368c0
,就是我们上面定义的globalQueue
,默认优先级的全局队列;
1.4 dispatch_queue_attr_t属性
对于dispatch_queue_create
方法,除了接受两个预定义的类型,还接受dispatch_queue_attr_t
类型,而GCD中提供了dispatch_queue_attr_t
类型的三种构造方法,因为本章主要研究的是队列的优先级,所以采用:dispatch_queue_attr_make_with_qos_class
方法来构造属性
- 测试代码:
// 串行队列(指定优先级)
dispatch_queue_attr_t serialAttr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
QOS_CLASS_USER_INTERACTIVE,
-1);
dispatch_queue_t userInteractiveQueue = dispatch_queue_create("com.xy.interactive.serialQueue", serialAttr);
NSLog(@"userInteractiveQueue : %@", userInteractiveQueue);
-
接受参数:
-
dispatch_queue_attr_t attr
:看起来还是它本身,但从注释中给的例子、以及前面看到的两个预定义的参数来说,这里应该填:DISPATCH_QUEUE_SERIAL
或DISPATCH_QUEUE_CONCURRENT
; -
dispatch_qos_class_t
:注释中也明确的指出,它只接受5种类型,其他类型都会返回NULL
@param qos_class A QOS class value: - QOS_CLASS_USER_INTERACTIVE - QOS_CLASS_USER_INITIATED - QOS_CLASS_DEFAULT - QOS_CLASS_UTILITY - QOS_CLASS_BACKGROUND Passing any other value is undefined.
虽然知道它表示的是优先级,然而是怎么表示优先级的并不知道,那就继续探索,最后找到了这些枚举值的具体值的定义,有了大小,结合名称定义上来看,数值大的,优先级更高。
__QOS_ENUM(qos_class, unsigned int, QOS_CLASS_USER_INTERACTIVE __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x21, QOS_CLASS_USER_INITIATED __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x19, QOS_CLASS_DEFAULT __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x15, QOS_CLASS_UTILITY __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x11, QOS_CLASS_BACKGROUND __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x09, QOS_CLASS_UNSPECIFIED __QOS_CLASS_AVAILABLE(macos(10.10), ios(8.0)) = 0x00, );
细心观察以下数量,出去最后一种
QOS_CLASS_UNSPECIFIED
类型,还有5种类型,乘以2,也才10种,距离12种还差两种。-
int relative_priority
:从命名上来看,可以理解为关联优先级,或者是辅助优先级判断。注释中给出的取值范围是:[0, QOS_MIN_RELATIVE_PRIORITY]
、#define QOS_MIN_RELATIVE_PRIORITY (-15)
,==>[0, -15]
。第二个参数的优先级是针对系统定义的12种根队列中的某一种,而我们可以创建非常多依赖某一个根队列中的队列,那这些优先级相同的队列之间应该也要有个优先级关系,使用该属性区分就对了,后面在源码中也可以看到这个属性。
-
输出结果:
po userInteractiveQueue
<OS_dispatch_queue_serial: com.xy.interactive.serialQueue[0x100605020] = { xref = 1, ref = 1, sref = 1, target = com.apple.root.user-interactive-qos.overcommit[0x100336b40], width = 0x1, state = 0x001ffe2000000000, in-flight = 0}>
- 一种新的类型:
com.apple.root.user-interactive-qos.overcommit
,而且这个地址,在前面打印的时候是没有的,现在有队列添加到上面的时候,地址中才有内容; - 根队列的地址空间是固定的,尽管那个位置指定队列没有用到,也不会分配给其他对象使用;
- 根队列中有添加队列时,则会动态的激活该地址,使该固定地址指向固定的根队列;
- 这种数据结构看起来像数组;
1.5 验证更多类型的队列
- 验证代码:
// 主队列(默认优先级)
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// 全局队列(默认优先级)
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 全局队列(高优先级)
dispatch_queue_t globalQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
// 全局队列(低优先级)
dispatch_queue_t globalQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
// 并行队列(默认优先级)
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.xy.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
// 串行队列(默认优先级)
dispatch_queue_t serialQueue = dispatch_queue_create("com.xy.serialQueue", DISPATCH_QUEUE_SERIAL);
// 串行队列(指定优先级)
dispatch_queue_attr_t serialAttr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
QOS_CLASS_USER_INTERACTIVE,
-1);
dispatch_queue_t userInteractiveQueue = dispatch_queue_create("com.xy.userInitiatedQueue.serialQueue", serialAttr);
dispatch_queue_attr_t userInitiatedAttr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT,
QOS_CLASS_USER_INITIATED,
-1);
dispatch_queue_t userInitiatedQueue = dispatch_queue_create("com.xy.userInitiatedQueue.serialQueue", userInitiatedAttr);
dispatch_queue_attr_t utilityAttr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
QOS_CLASS_UTILITY,
-1);
dispatch_queue_t utilityQueue = dispatch_queue_create("com.xy.utilityQueue.serialQueue", utilityAttr);
- 输出结果:
(lldb) po 0x100336540
<OS_dispatch_queue_mgr: com.apple.libdispatch-manager[0x100336540] = { xref = -2147483648, ref = -2147483648, sref = 1, target = com.apple.root.user-interactive-qos.overcommit[0x100336b40], width = 0x1, state = 0x001ffe1000000000, in-flight = 0}>
(lldb) po 0x100336640
<OS_dispatch_queue_global: com.apple.root.maintenance-qos.overcommit[0x100336640] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336740
<OS_dispatch_queue_global: com.apple.root.background-qos.overcommit[0x100336740] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336840
<OS_dispatch_queue_global: com.apple.root.utility-qos.overcommit[0x100336840] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336940
<OS_dispatch_queue_global: com.apple.root.default-qos.overcommit[0x100336940] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336a40
<OS_dispatch_queue_global: com.apple.root.user-initiated-qos.overcommit[0x100336a40] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336b40
<OS_dispatch_queue_global: com.apple.root.user-interactive-qos.overcommit[0x100336b40] = { xref = -2147483648, ref = -2147483648, sref = 1, target = [0x0], width = 0xfff, state = 0x0060000000000000, in-barrier}>
(lldb) po 0x100336c40
4298337344
(lldb) po 0x100336d40
4298337600
(lldb) po 0x100336e40
4298337856
(lldb) po 0x100336f40
4298338112
- 多了一种类型:
com.apple.root.user-initiated-qos.overcommit
- 根队列之间的顺序是相对的,但位置不是绝对的。(推翻上面的猜测:
根队列的地址空间是固定的
)
二、总结
2.1 获取队列的三种方式
- dispatch_get_main_queue()
- dispatch_get_global_queue()
- dispatch_queue_create()
2.2 队列的优先级
- 上面三种方法获取的队列都是默认优先级的;
- 可以通过
dispatch_queue_attr_make_with_qos_class
方法创建指定优先级的属性; - 系统提供12种根队列,所有的非根队列都依赖其中一个根队列。可以参考为父视图与子视图之间的关系;
-
__QOS_ENUM
指定的优先级是target的优先级,表示你希望你的队列依赖系统的哪个优先级的队列; -
relative_priority
表示队列在根队列中的优先级,取值范围[0,-15]
。一个根队列上会有很多子队列,而这些子队列之间也存在优先级关系,就用这个属性标识。
2.3 更多疑问
- 是否可以将一个队列的target设置为自定义队列,而不是根队列?
当然可以。看系统API:dispatch_set_target_queue(dispatch_object_t, dispatch_queue_t)
。
-
in-barrier
是什么意思?在上述队列的描述中,有的有in-barrier
,有的没有,这个属性标识着什么特性呢?期待下回分解……