Can you request a serial GCD queue for the number of pending taks?

I take over the management of a client application written by one of the employees of the client who was new to iOS development (and has since left the company)

I am trying to get / improve using concurrency. This created many different GCD timers with different delays, and time was messy.

I'm probably going to convert it to use a sequential GCD queue, since I need the tasks to be executed in sequential order (but not in the main thread.) I would like to keep track of how deep pending tasks get in the queue. NSOperationQueues has a tool for finding the number of pending tasks, but I don't see a similar option for serial GCD queues. Is there such a tool?

I suppose that I could build an NSOperationQueue and make each operation dependent on the previous operation, thus creating a sequence of serial work, but work a lot just to diagnose the number of tasks that are in my queue at any given time.

+4
source share
1 answer

There is no public API to request the number of tasks in the GCD queue. You can either create something yourself, or use it NSOperationQueue. If you want to create something, you can wrap dispatch_a/syncit up to increase the counter, and then set another block with each operation, which decreases the counter, for example:

int64_t numTasks = 0

void counted_dispatch_async(dispatch_queue_t queue, dispatch_block_t block)
{
    OSAtomicIncrement64(&numTasks);
    dispatch_async(queue, ^{
        block();
        OSAtomicDecrement64(&numTasks);
    });
}

NSOperationQueue. , . : " , ".

+8

All Articles