Swift: Limit CPU time of thread, queue or operation

Viewed 56

I employ a native C library in my iOS app. Calling the code directly puts it on the main thread (of course depending on from where I call it) so I have created a new queue in order to leave the main thread for the call.

let cProcessingQueue = DispatchQueue(label: "cProcessingQueue")


cProcessingQueue.async {
    //call that launches the c operation
}

This keeps the UI responsive. However, on larger data sets this has the potential to throw crashes that contain statements like the following:

Action taken:    Process killed
CPU:             48 seconds cpu time over 54 seconds (88% cpu average), exceeding limit of 80% cpu over 60 seconds

Is there I can apply to have some kind of hard(ish) limit like "qos: .background will always limit the usage to 50%" in order to slow down the C lib execution in first place? I'm fine with using NSOperation or whatever there is but that would probably the most convenient solution.

A few other ideas I can think of are:

  1. Feeding smaller chunks of data with some delay in between, however then we have arbitrary wait times that go unused
  2. Watching the CPU time as shown in Get CPU usage IOS Swift and modifying the C library to support halting executions

but all in all, the "thread-related limit" would definitely the easiest solution out there.

0 Answers
Related