Does dispatch_group_wait allow the line to be cut?

This question concerns Grand Central Dispatch and dispatch_group_wait()

in particular.

Suppose what's dispatch_group

called group

with 10 tasks in it waiting to be done.

Elsewhere, I have a task that needs to wait for any tasks in group

to complete before doing it. To make this happen, I use dispatch_group_wait(group, DISPATCH_TIME_FOREVER)

.

To distinguish it from tasks in group

, I'll name it lonelyTask

.

If another task is added to group

while lonelyTask

waiting, which one is first executed, lonelyTask

or a task added to group

? In other words, execute tasks added to group

while another task is waiting to be executed to get a "cut line" in front of the pending task, or is the order in which they were called is maintained?

I've searched the documentation but couldn't find an answer to this question ...

+3


source to share


2 answers


The group is a +/- counter (semaphore) that fires every time it reaches zero. It doesn't know the tasks because dispatch_group_async () is just a wrapper that enters () the group s before submitting the task and puts in a new block that will invoke the task block and then leave () that group. All this. Groups can even be used without queuing in the form of asynchronous hold counters or sml.

So in general "you can't".

But if you [re] target (dispatch_set_target_queue ()) all related queues into one parallel queue, then dispatch_barrier_async () on that queue can do what you want, if called in the correct isolation. Note that the group has nothing to do with this.



Also, the original lonelyTask is not a task from a group perspective - it is an unrelated thread of execution that waits until the group is balanced. A synonym for "cut to line" is "barrier".


Another approach would be to create a private group for each set of tasks and wait for that specific group. This will not insert a barrier, although the following tasks will not wait for the lonelyTask to complete, and any uncontrolled async () s could "break the rules" resulting in contiguous debugging sessions.

+2


source


dispatch_group_wait

and dispatch_group_notify

both wait until the number of items that are included in the group goes to zero. So, if you add an eleventh task to the group before all of the original ten tasks are complete, the call dispatch_group_wait

will wait for all eleven to complete before continuing.



+3


source







All Articles