Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dispatch_group_wait allow cutting in line?

This question is about Grand Central Dispatch, and dispatch_group_wait() in particular.

Assume a dispatch_group called group with 10 tasks in it waiting to be performed.

Elsewhere I have a task that needs to wait for any tasks in group to finish before it executes. To make that happen, I use dispatch_group_wait(group, DISPATCH_TIME_FOREVER).

To distinguish it from the tasks in group I'll call it lonelyTask.

If another task gets added to group while lonelyTask is waiting, which gets executed first, lonelyTask or the task that was added to group? In other words, do tasks added to a group while another task is waiting to execute get to "cut in line" ahead of the waiting task, or is the order in which they were called maintained?

I have searched the documentation, but haven't been able to find an answer to this question...

like image 482
Aaron Rasmussen Avatar asked Dec 05 '25 21:12

Aaron Rasmussen


1 Answers

dispatch_group_wait and dispatch_group_notify both wait for the number of items that have entered the group to transition to zero. So, if you add an eleventh task to the group before all of the original ten tasks complete, a call to dispatch_group_wait will wait for all eleven to complete before continuing.

like image 85
ipmcc Avatar answered Dec 08 '25 13:12

ipmcc