dispatch_group_t
dispatch_group 会在整个组的任务全部完成时通知开发者,这些任务可以同步也可以异步,甚至不同队列。
需要搭配几个关键函数
dispatch_group_enter(group); 进入该组
dispatch_group_leave(group);离开改组
dispatch_group_notify 组内任务都完成通知给主线程进行刷新UI等操作
- (void)group_display_action{
//创建一个调度组
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t theGlobalQueue = dispatch_get_global_queue(0, 0);
dispatch_queue_t theSerialQueue = dispatch_queue_create("serial.queue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t theConQueue = dispatch_queue_create("concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
//将任务丢进调度组
dispatch_group_async(group, theGlobalQueue, ^{
NSLog(@"task1 start--------");
sleep(2);
NSLog(@"task1 finished--------");
});
dispatch_group_async(group, theSerialQueue, ^{
NSLog(@"task2 start--------");
sleep(4);
NSLog(@"task2 finished--------");
});
dispatch_group_enter(group);
dispatch_group_async(group, theConQueue, ^{
NSLog(@"task3 start--------");
sleep(5);
NSLog(@"task3 finished--------");
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all tasks are finished!11");
});
NSLog(@"dispatch_group_notify is async 😊");
}
2020-10-30 09:30:44.229254+0800 GCDPropertyQueue[3519:303051] dispatch_group_notify is async 😊
2020-10-30 09:30:44.229243+0800 GCDPropertyQueue[3519:303253] task2 start--------
2020-10-30 09:30:44.229263+0800 GCDPropertyQueue[3519:303252] task1 start--------
2020-10-30 09:30:44.229269+0800 GCDPropertyQueue[3519:303254] task3 start--------
2020-10-30 09:30:46.233023+0800 GCDPropertyQueue[3519:303252] task1 finished--------
2020-10-30 09:30:48.232402+0800 GCDPropertyQueue[3519:303253] task2 finished--------
2020-10-30 09:30:49.232947+0800 GCDPropertyQueue[3519:303254] task3 finished--------
2020-10-30 09:30:49.233677+0800 GCDPropertyQueue[3519:303051] all tasks are finished!11
dispatch_group_enter 源码
void
dispatch_group_enter(dispatch_group_t dg)
{
// The value is decremented on a 32bits wide atomic so that the carry
// for the 0 -> -1 transition is not propagated to the upper 32bits.
uint32_t old_bits = os_atomic_sub_orig2o(dg, dg_bits,
DISPATCH_GROUP_VALUE_INTERVAL, acquire);
uint32_t old_value = old_bits & DISPATCH_GROUP_VALUE_MASK;
if (unlikely(old_value == 0)) {
_dispatch_retain(dg); // <rdar://problem/22318411>
}
if (unlikely(old_value == DISPATCH_GROUP_VALUE_MAX)) {
DISPATCH_CLIENT_CRASH(old_bits,
"Too many nested calls to dispatch_group_enter()");
}
}
dispatch_group_leave源码
void
dispatch_group_leave(dispatch_group_t dg)
{
// The value is incremented on a 64bits wide atomic so that the carry for
// the -1 -> 0 transition increments the generation atomically.
uint64_t new_state, old_state = os_atomic_add_orig2o(dg, dg_state,
DISPATCH_GROUP_VALUE_INTERVAL, release);
uint32_t old_value = (uint32_t)(old_state & DISPATCH_GROUP_VALUE_MASK);
if (unlikely(old_value == DISPATCH_GROUP_VALUE_1)) {
old_state += DISPATCH_GROUP_VALUE_INTERVAL;
do {
new_state = old_state;
if ((old_state & DISPATCH_GROUP_VALUE_MASK) == 0) {
new_state &= ~DISPATCH_GROUP_HAS_WAITERS;
new_state &= ~DISPATCH_GROUP_HAS_NOTIFS;
} else {
// If the group was entered again since the atomic_add above,
// we can't clear the waiters bit anymore as we don't know for
// which generation the waiters are for
new_state &= ~DISPATCH_GROUP_HAS_NOTIFS;
}
if (old_state == new_state) break;
} while (unlikely(!os_atomic_cmpxchgv2o(dg, dg_state,
old_state, new_state, &old_state, relaxed)));
return _dispatch_group_wake(dg, old_state, true);
}
if (unlikely(old_value == 0)) {
DISPATCH_CLIENT_CRASH((uintptr_t)old_value,
"Unbalanced call to dispatch_group_leave()");
}
}
dispatch_group_notify源码
DISPATCH_NOINLINE
void
dispatch_group_notify_f(dispatch_group_t dg, dispatch_queue_t dq, void *ctxt,
dispatch_function_t func)
{
dispatch_continuation_t dsn = _dispatch_continuation_alloc();
_dispatch_continuation_init_f(dsn, dq, ctxt, func, 0, DC_FLAG_CONSUME);
_dispatch_group_notify(dg, dq, dsn);
}
#ifdef __BLOCKS__
void
dispatch_group_notify(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_block_t db)
{
dispatch_continuation_t dsn = _dispatch_continuation_alloc();
_dispatch_continuation_init(dsn, dq, db, 0, DC_FLAG_CONSUME);
_dispatch_group_notify(dg, dq, dsn);
}
static inline void
_dispatch_group_notify(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_continuation_t dsn)
{
uint64_t old_state, new_state;
dispatch_continuation_t prev;
dsn->dc_data = dq;
_dispatch_retain(dq);
prev = os_mpsc_push_update_tail(os_mpsc(dg, dg_notify), dsn, do_next);
if (os_mpsc_push_was_empty(prev)) _dispatch_retain(dg);
os_mpsc_push_update_prev(os_mpsc(dg, dg_notify), prev, dsn, do_next);
if (os_mpsc_push_was_empty(prev)) {
os_atomic_rmw_loop2o(dg, dg_state, old_state, new_state, release, {
new_state = old_state | DISPATCH_GROUP_HAS_NOTIFS;
if ((uint32_t)old_state == 0) {
os_atomic_rmw_loop_give_up({
return _dispatch_group_wake(dg, new_state, false);
});
}
});
}
}
关键方法_dispatch_group_wake源码
DISPATCH_NOINLINE
static void
_dispatch_group_wake(dispatch_group_t dg, uint64_t dg_state, bool needs_release)
{
uint16_t refs = needs_release ? 1 : 0; // <rdar://problem/22318411>
if (dg_state & DISPATCH_GROUP_HAS_NOTIFS) {
dispatch_continuation_t dc, next_dc, tail;
// Snapshot before anything is notified/woken <rdar://problem/8554546>
dc = os_mpsc_capture_snapshot(os_mpsc(dg, dg_notify), &tail);
do {
dispatch_queue_t dsn_queue = (dispatch_queue_t)dc->dc_data;
next_dc = os_mpsc_pop_snapshot_head(dc, tail, do_next);
_dispatch_continuation_async(dsn_queue, dc,
_dispatch_qos_from_pp(dc->dc_priority), dc->dc_flags);
_dispatch_release(dsn_queue);
} while ((dc = next_dc));
refs++;
}
if (dg_state & DISPATCH_GROUP_HAS_WAITERS) {
_dispatch_wake_by_address(&dg->dg_gen);
}
if (refs) _dispatch_release_n(dg, refs);
}
另一种方案dispatch_group_wait
同步执行,会阻塞线程,所有任务完成之前该方法会一直阻塞线程
代码实现
- (void)group_wait_action{
//创建一个调度组
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t theGlobalQueue = dispatch_get_global_queue(0, 0);
dispatch_queue_t theSerialQueue = dispatch_queue_create("serial.queue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t theConQueue = dispatch_queue_create("concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
//将任务丢进调度组
dispatch_group_async(group, theGlobalQueue, ^{
NSLog(@"task1 start--------");
sleep(2);
NSLog(@"task1 finished--------");
});
dispatch_group_async(group, theSerialQueue, ^{
NSLog(@"task2 start--------");
sleep(4);
NSLog(@"task2 finished--------");
});
dispatch_group_enter(group);
dispatch_group_async(group, theConQueue, ^{
NSLog(@"task3 start--------");
sleep(5);
NSLog(@"task3 finished--------");
dispatch_group_leave(group);
});
NSLog(@"dispatch_group_wait 即将困住线程");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"dispatch_group_wait 释放线程");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"all taks are finished!!");
});
}
2020-10-30 10:03:23.886765+0800 GCDPropertyQueue[3650:324430] task1 start--------
2020-10-30 10:03:23.886764+0800 GCDPropertyQueue[3650:324432] task2 start--------
2020-10-30 10:03:23.886765+0800 GCDPropertyQueue[3650:324435] task3 start--------
2020-10-30 10:03:23.886778+0800 GCDPropertyQueue[3650:324284] dispatch_group_wait 即将困住线程
2020-10-30 10:03:25.888278+0800 GCDPropertyQueue[3650:324430] task1 finished--------
2020-10-30 10:03:27.891935+0800 GCDPropertyQueue[3650:324432] task2 finished--------
2020-10-30 10:03:28.891541+0800 GCDPropertyQueue[3650:324435] task3 finished--------
2020-10-30 10:03:28.892145+0800 GCDPropertyQueue[3650:324284] dispatch_group_wait 释放线程
2020-10-30 10:03:28.909946+0800 GCDPropertyQueue[3650:324284] all taks are finished!!