2

Dispatch groups are a GCD feature the allows one to submit blocks to be dispatched to certain queues. Regarding the queues, the blocks are dispatched acording to the queue's type: if a queue is serial, the block is going to be executed serially in relation to that queue; if a queue is concurrent, the same happens, but concurrently.

However, regarding the group, dispatches occur serially or concurrently in relation to each other? I mean, if a group has a queue of dispatch queues and blocks to be dispatched, is the next dispatch executed only when the previous one gets finished?

4

1 回答 1

8

调度组中的成员资格与执行顺序无关。

调度组实际上只是一个计数器,dispatch_group_enter(group)递增计数器并 dispatch_group_leave(group)递减计数器。

dispatch_group_async(group, queue, block)是一个快捷方式:

dispatch_group_enter(group);
dispatch_async(queue, ^{
    block();
    dispatch_group_leave(group);
});

即通过提交的块的执行顺序dispatch_group_async(group, queue, block)仅取决于queue指定的(s)。

当调度组计数器达到零(组变为空)时,所有等待者dispatch_group_wait(group)都被唤醒,并且已经提交给组的任何块都被dispatch_group_notify(group, queue, block)异步到它们各自queue的s(即这些块可以并发执行,如果指定queue的 s 是并发/独立的)。

于 2013-05-20T16:09:04.740 回答