11--多线程05--GCD队列优先级

2021-07-19  本文已影响0人  修_远

想了许久,还是决定将GCD的内容尽量写得简单一点,所以多分几个章节,每个章节内容尽量少。

一、如何创建一个队列

GCD提供了3个创建队列的API,下面就通过这是三个API来看看队列的优先级中的那些事。

1.1 dispatch_get_main_queue()

dispatch_queue_t mainQueue = dispatch_get_main_queue();
(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 }>
  1. OS_dispatch_queue_main: com.apple.main-thread[0x1003364c0]:主队列队列类型:OS_dispatch_queue_main,标签:com.apple.main-thread[0x1003364c0]:队列的地址;
  2. xref/ref/sref = 1:ref一般表示引用,只要是对象,都满足ARC里面的引用计数规则,所以这三个属性应该是表示某种引用计数。但这个1表示的是自己的引用计数,还是target的引用计数还无法确定,答案可能藏在后面的分析中;
  3. target = com.apple.root.default-qos.overcommit[0x100336940]:让人耳目一新的属性出来的,主队列仍然是依赖于某个队列,这个队列又有啥特点呢?
  4. width = 0x1:这个简单,串行队列,宽度为1,表示任务只能逐个执行;
  5. state = 0x001ffe1000000304:是一个地址,因为对内存地址的取值不太清楚😣,所以这里也无法确定是一个对象地址,还是表示一个值,希望后面的分析中还能看到;
  6. in-flight = 0:第一次见,这里也不做展开分析,重点是队列的优先级;
  7. thread = 0x307:众所周知,系统会给主队列分配一个主线程;
(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}>
  1. OS_dispatch_queue_global:主线程所依赖的队列是一个全局队列,有点意思😁;
  2. target:target为空,表示已经没有所依赖的队列了;

1.2 dispatch_get_global_queue()

(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}>
  1. 这是三个不同的队列,而且地址是相邻的,间距128字节,可能是队列size,;
  2. 这三个队列都是OS_dispatch_queue_global类型的队列,而且target都为空,有可能是根队列。主队列所依赖的队列地址为:0x100336940,与0x1003367c0中间还有两个间距,将它们打印出来瞅瞅;
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}>
  1. 结果不负众望,确实有两个队列,而且队列标签还不尽相同,分别是:com.apple.root.maintenance-qoscom.apple.root.background-qos
  2. 既然有规律,那就按照这个规律打印更多的地址试试看;
 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
  1. 很可惜,只找到了地址:0x100336ac0所对应的队列,听说有12种,但目前只看到了6种,会不会是我们创建的队列不够多,而根队列的加载方式属于懒加载,所以才无法找到其他的几条队列呢?
  2. 这个问题就放在第二轮,创建更多类型的队列之后再验证。

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);
(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}>
  1. 串行队列serialQueue的目标队列0x100336940,跟主队列的目标队列是同一个;
  2. 并行队列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_make_with_
// 串行队列(指定优先级)
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);
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}>
  1. 一种新的类型:com.apple.root.user-interactive-qos.overcommit,而且这个地址,在前面打印的时候是没有的,现在有队列添加到上面的时候,地址中才有内容;
  2. 根队列的地址空间是固定的,尽管那个位置指定队列没有用到,也不会分配给其他对象使用;
  3. 根队列中有添加队列时,则会动态的激活该地址,使该固定地址指向固定的根队列;
  4. 这种数据结构看起来像数组;

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
  1. 多了一种类型:com.apple.root.user-initiated-qos.overcommit
  2. 根队列之间的顺序是相对的,但位置不是绝对的。(推翻上面的猜测:根队列的地址空间是固定的

二、总结

2.1 获取队列的三种方式

  1. dispatch_get_main_queue()
  2. dispatch_get_global_queue()
  3. dispatch_queue_create()

2.2 队列的优先级

  1. 上面三种方法获取的队列都是默认优先级的;
  2. 可以通过dispatch_queue_attr_make_with_qos_class方法创建指定优先级的属性;
  3. 系统提供12种根队列,所有的非根队列都依赖其中一个根队列。可以参考为父视图与子视图之间的关系;
  4. __QOS_ENUM指定的优先级是target的优先级,表示你希望你的队列依赖系统的哪个优先级的队列;
  5. relative_priority表示队列在根队列中的优先级,取值范围[0,-15]。一个根队列上会有很多子队列,而这些子队列之间也存在优先级关系,就用这个属性标识。

2.3 更多疑问

  1. 是否可以将一个队列的target设置为自定义队列,而不是根队列?

当然可以。看系统API:dispatch_set_target_queue(dispatch_object_t, dispatch_queue_t)

  1. in-barrier是什么意思?在上述队列的描述中,有的有in-barrier,有的没有,这个属性标识着什么特性呢?期待下回分解……
上一篇 下一篇

猜你喜欢

热点阅读