简体   繁体   中英

How to get the tasks list in GCD queue?

I get the main queue in GCD as below and I add different tasks from different classes in my apps.

dispatch_queue_t queue = dispatch_get_global_queue (
    DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

Now I want to know how many of my tasks which are still in the GCD main queue.

Is there any method to get the tasks list in GCD queue?

Thanks

That's really not the paradigm of GCD. If you want to keep track of a certain group of operations, for example, you can create a dispatch group and sign up to be notified when it is done, as in this example.

dispatch_group_t taskGroup = dispatch_group_create();
dispatch_queue_t queue = //Get whatever queue you want here
dispatch_group_async(taskGroup, queue, ^ {
    [object doSomething];
});
dispatch_group_async(taskGroup, queue, ^ {
    [object doMoreStuff];
});
dispatch_group_async(taskGroup, queue, ^ {
    [object doEvenMoreStuff];
});
dispatch_group_notify(taskGroup, queue, ^{
    [object workDone];
});
dispatch_release(taskGroup);

Typically this is done with dispatch groups rather than queues. You can assign tasks to a group using dispatch_group_async() , or you can manually mark things in the group using dispatch_group_enter() and dispatch_group_leave() . You can then check for whether there is anything in the group using either dispatch_group_notify() or dispatch_group_wait() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM