带有qos_class_user_interactive的global_queue [英] global_queue with qos_class_user_interactive

查看:86
本文介绍了带有qos_class_user_interactive的global_queue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解GCD,并编写了以下代码以找出运行优先级:

I try to understand GCD and wrote this code to find out run priority:

  override func viewDidLoad() {
    super.viewDidLoad()

    fetchImage()
    print(1)

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
      print(2)
    }

    dispatch_async(dispatch_get_main_queue()) {
      print(3)
    }
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) { 
      print(5)
    }
  }

我在控制台中得到了下一个结果:

I got next result in the console:

1

2

5

3

所以问题是:

第1部分:为什么3表示5之后(main_queue具有最高优先级?)

Part 1: Why 3 is after 5 (main_queue has highest priority?)

第2部分:为什么2也要比3和5高?

Part 2: And why 2 is higher that 3 and 5 as well?

谢谢你们!

推荐答案

注意:这是在多核设备上进行多线程处理,将输出写入日志,您不知道线程的安全性和内部性. ...的管理

Bear in mind: this is multi-threading, on a multi-core device, writing output to a log that you don't know the thread safety and internal management of...

说:

  1. 第一个是因为它是同步的
  2. 2是第二个因为它也是同步的
  3. 3不是下一个,因为它被放入等待在主线程运行循环中运行的事物队列中,并且您不知道该队列中已经有什么其他物体了
  4. 5在3之前,因为它(基本上)具有相同的优先级,但是它正在队列中运行,可能没有其他等待(QOS_CLASS_USER_INTERACTIVE〜=主线程优先级)
  1. 1 is first because it's synchronous
  2. 2 is second because it's also synchronous
  3. 3 is not next because it's pushed into the queue of things waiting to run on the main thread run loop and you don't know what else is already in that queue
  4. 5 is before 3 because it's (basically) the same priority but it's running on a queue that probably does't have anything else waiting (QOS_CLASS_USER_INTERACTIVE ~= main thread priority)

请注意,我说〜=是因为我没有检查确切的值,尽管我希望优先级值能够匹配,但它可能会略有不同,否则'interactive'的意义不大...

Note, I say ~= because I haven't checked the exact values and it may differ slightly though I expect the priority values to match, otherwise 'interactive' wouldn't mean much...

这篇关于带有qos_class_user_interactive的global_queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆