IOS thread lag in background using GCD

IOS 11.3 I'm experiencing a definite lag whilst using a thread (even one with NSQualityOfServiceUserInteractive) with high activity (5ms processing)


Start the loop here

NSOperationQueue *q = [[NSOperationQueue alloc] init];

q.qualityOfService=NSQualityOfServiceUserInteractive; // highest priority

[q addOperationWithBlock: ^{

[self runRackPanelLoopThread];

}];


Then runRackPanelLoopThread sits in a while loop looking at CACurrentMediaTime() & measuring intervals to fire off events.


But, when my app switches to background the thread created glitches - it gets starved of cpu (you can see it drop even with the xcode cpu monitor).


Stack trace with pointers shows a definite gap during this glitch ( nice and regular when in foregound).


I've tried many forms of dispatch but all the same, the only method I have that works is an AudioUnit callback to call my processing, but thats limited to about 23ms.


I can't run my processing on the main thread as it kills the app, and dispatch to main queue also kills it - it has to be in non-gcd code.


Is there a bug with IOS11 that means any GCD threads are preempted for a long time (I've seen up to .25 seconds) whilst in background and the system is being loaded.


I should point out that this is really bad on A7 devices, but less noticeable as you improve the cpu (doesn't happen on ipad pro)


What should I be looking for?


regards

Tony

Replies

You might want to try using the System Trace template in Instruments. When you see the performance glitch, press stop and look at the results. The "System Load" instrument will allow you to see the scheduling table under the blue inspection head, so it's a good way to find when your code is being preempted, and what is preempting it. That will at least let you test the hypothesis that the OS is reducing the priority of your threads when in the backaground.