1

I'm learning Swift concurrency and encountered this excerpt

“If you submit a task with a higher quality of service than the queue has, the queue’s level will increase. Not only that, but all the operations enqueued will also have their priority raised as well.”

As stated in the quote, I am trying to experiment it simulate that a queue with lower QoS will be increased if we submit a task with higher QoS. Here's my current code snippet:

let userInitiatedQueue = DispatchQueue.global(qos: .userInitiated)
let utilityQueue = DispatchQueue.global(qos: .utility)

userInitiatedQueue.async {
  print("log from userInitiatedQueue")

  utilityQueue.async {
    // ❗️debugging at this point, checking `utilityQueue.qos` is still `.utility`
    print("log from utilityQueue, it should increase the QoS")
  }
}

// Submitting a `DispatchWorkItem` doesn't work either
let workItem = DispatchWorkItem(qos: .userInitiated) {
// ❗️debugging at this point, checking `utilityQueue.qos` is still `.utility`
  print("log workItem from utilityQueue, it should increase the QoS")
}
utilityQueue.async(execute: workItem)

Could you provide a snippet that will prove the concept above?

chlkdst
  • 175
  • 3
  • 13
  • Can you show the code that actually enqueues `workItem` onto the queue? – Itai Ferber Oct 25 '22 at 16:32
  • 1
    Re the first example, dispatching asynchronously from high QoS queue to a low QoS queue should not elevate the QoS. – Rob Oct 25 '22 at 19:56
  • 1
    Re the second example, adding a `userInitiated` work item to a `utility` queue (which I presume you did, but accidentally omitted from question) should, theoretically, elevate the QoS of the queue (and anything pending on that queue), but I am seeing the same behavior, namely that the `qos` property does not reflect any changing priority. I glanced at the notoriously dense [libDispatch source](https://github.com/apple/swift-corelibs-libdispatch), with the hope of finding some obvious QoS elevation code, but nothing jumped out at me. – Rob Oct 25 '22 at 20:03
  • 1
    If it helps, the [Energy Efficiency Guide for iOS Apps: Prioritize Work with Quality of Service Classes](https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html) outlines the QoS elevation logic, though admittedly this old doc uses the old Swift 2 GCD API. – Rob Oct 25 '22 at 20:06

0 Answers0