Reputation: 47354
I'm using a serial dispatch queue to alter images coming from the camera for a timelapse video app. So far I'm using GCD to offload image processing to a second thread.
I expect the app to be running for a long time and don't want to somehow overwhelm the device with processing requests.
Is there a way to check if the dispatch queue cannot keep up with the number of operations added to it (creating a backlog)?
dispatch_async(backgroundQueue, ^{
__block UIImage* backupImage = self.thermalImage;
backupImage = [self imageByDrawingCircleOnImage:backupImage];
dispatch_async(dispatch_get_main_queue(), ^{
[self.thermalImageView setImage:backupImage];
});
});
Upvotes: 3
Views: 159
Reputation: 452
Do you have a specific number of tasks you want to execute in this queue before deciding that it's backlogged? If so, use a semaphore
to limit the number of tasks in the queue.
dispatch_semaphore_t semaphore = dispatch_semaphore_create(3);
dispatch_queue_t queue = dispatch_queue_create("com.foo.queue", DISPATCH_QUEUE_CONCURRENT);
for(int i = 1; i < 7; i++) {
dispatch_async(queue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"hello from #%d", i);
[NSThread sleepForTimeInterval:2];
dispatch_semaphore_signal(semaphore);
});
}
You can specify a timeout and then check the return value of dispatch_semaphore_wait
to see if you timed out and then create a new queue or just wait until the queue frees up etc.
Upvotes: 5