Reputation: 103
I'm trying to run three concurrent tasks that each take ~30 seconds. However only two will run at the same time, and the third task waits for one of those to complete before it begins.
My current code:
func sliderMoved(_ label : UILabel, openCharacteristicNum : Int, closeCharacteristicNum : Int, sender: UISlider, sliderTimer:Timer){
//run this on a background thread
DispatchQueue.global(qos: .utility).async {
var actuatorPosition = self.getActuatorPosition(label)
if (actuatorPosition < sender.value) {
// stop closing (if it was) and start opening
self.writeValue(closeCharacteristicNum, value: 1, withResponse: false)
self.writeValue(openCharacteristicNum, value: 0, withResponse: false)
while actuatorPosition < senderValue && sliderTimer.isValid{
actuatorPosition = self.getActuatorPosition(label)
}
// stop opening
self.writeValue(openCharacteristicNum, value: 1, withResponse: false)
}
}
}
I've also tried creating a concurrent queue and running three tasks within that queue, but still only two run concurrently. I've also tried creating three separate concurrent queues and running one task in each, but still had the same problem.
My only guess is that I'm hitting a hard thread limit that iOS allows the app to use, seeing as each task completely blocks the thread until complete.
Upvotes: 0
Views: 579
Reputation: 189
Multicore iOS devices only have 2-cores with 1-thread each, so the limit is 2-threads at once. The only exception is the iPad Air 2 which had 3-cores.
Edit: This is no longer the case. Since A10 it has been 2 big and 2 or 4 small cores. As far as I know, Apple's A-series still only support one hardware thread per core. That said, libDispatch will manage the thread pool for you and it should pick the optimum number of threads for the iOS device it is running on, the number of hardware threads available is a low level detail that you shouldn't worry about when using libDispatch.
Upvotes: 1
Reputation: 536047
but still had the same problem
Except that there is no "problem" here.
My only guess is that I'm hitting a hard thread limit that iOS allows the app to use, seeing as each task completely blocks the thread until complete
I wouldn't describe it as a "hard" limit, but there could be a limit. The point, however, is that there are no guarantees about anything so you shouldn't have any expectations one way or another. The whole point of GCD is that you concern yourself with queues and allow the kernel to allocate threads as it sees fit. And that is exactly what it is doing. You scheduled operations on the queue and they eventually got performed. Don't worry, be happy.
Upvotes: 0