Home > Software engineering >  Custom DispatchQueue quality of service
Custom DispatchQueue quality of service

Time:09-10

Is there a way to create a custom DispatchQueue quality of service with its own custom "speed"? For example, I want a QoS that's twice as slow as .utility.

Ideas on how to solve it

  • Somehow telling the CPU/GPU that we want to run the task every X operation cycles? Not sure if that's directly possible with iOS.
  • This is a really bad hack which produces messy code and doesn't really solve the issue if 1 line of code runs for several seconds, but we can introduce a wait after every line of code.
  • In SpriteKit/SceneKit, it's possible to slow down time. Is there a way to utilize that somehow to slow down an arbitrary piece of code?

CodePudding user response:

There is no mechanism in iOS or any other Cocoa platform to control the "speed" (for any meaning of that word) of a work item. The only tool offered us is some control over scheduling. Once your work item is scheduled, it will get 100% (*) of the CPU core until it ends or is preempted. There is no way to be asked to be preempted more often (and it would be expensive to allow that, since context switches are expensive).

The way to manage how much work is done is to directly manage the work, not preemption. The best way is to split up the work into small pieces, and schedule them over time and combine them at the end. If your algorithm doesn't support that kind of input segmentation, then the algorithm's main "loop" needs to limit the number of iterations it performs (or the amount of time it spends iterating), and return at that point to be scheduled later.

If you don't control the algorithm code, and you cannot work with whoever does, and you cannot slice your data into smaller pieces, this may be an unsolvable problem.

(*) With the rise of "performance" cores and other such CPU advances, this isn't completely true, but for this question it's close enough.

CodePudding user response:

Technically you cannot alter the speed on the QoS such as .background or .utility or any other Qos.

The way to handle this is to choose the right QoS based on the task you want to perform.

The higher the QoS is, the more resources the OS will spend on it and descends when you use a lower one.

  • Related