global_queue with qos_class_user_interactive

陌路散爱 提交于 2019-12-22 11:03:51

问题


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

So the question is:

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

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

Thank you guys!


回答1:


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...

That said:

  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)

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...



来源:https://stackoverflow.com/questions/36735946/global-queue-with-qos-class-user-interactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!