“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

The GCD queues group

Sometimes there is a need to execute two time-consuming tasks asynchronously and then return to the main thread when both tasks are completed. This is where the GCD queue group can be used

  • Create a queue group using dispatch_group_create
  • Use dispatch_group_async to add tasks to the queue

dispatch_group_notify

  • Monitors the task completion status in the group. After all tasks in the group are completed, a dispatch_group_notify notification is received
  • If dispatch_group_notify is used, the current thread is not blocked

Example:

Create a queue group
dispatch_group_t group = dispatch_group_create();

// Create a queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Put the task in the queue
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@"Task 1 - % @",[NSThread currentThread]); }});// Put the task in the queue
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@Task 2 - % @ "",[NSThread currentThread]); }});// This message is received when all queues in the queue group are complete
dispatch_group_notify(group, queue, ^{
    NSLog(@"1 and 2 completed -- %@",[NSThread currentThread]);
});

NSLog(@"Current thread --3--%@",[NSThread currentThread]);
Copy the code

The log:

dispatch_group_wait

  • Monitors the task completion status in the group. After all tasks in the group are completed, a dispatch_group_notify notification is received
  • If dispatch_group_notify is used, the current thread is blocked

Example:

Create a queue group
dispatch_group_t group = dispatch_group_create();

// Create a queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Put the task in the queue
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@"Task 1 - % @",[NSThread currentThread]); }});// Put the task in the queue
dispatch_group_async(group, queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@Task 2 - % @ "",[NSThread currentThread]); }});/ / set dispatch_group_wait
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

// Block the current thread until all queues in this queue group are complete
NSLog(@"Current thread --3--%@",[NSThread currentThread]);
Copy the code

The log:

Dispatch_group_enter and dispatch_group_leave

  • “Dispatch_group_enter” indicates that a task is added to a group. The value of “dispatch_group_enter” indicates that a task is added to a group. The value of “dispatch_group_enter” indicates that a task is added to a group

  • Dispatch_group_leave indicates that a task leaves the group. The value of dispatch_group_leave is equal to -1

  • Dispatch_group_enter and dispatch_group_leave are the same as dispatch_group_async

  • When the number of unexecuted tasks is 0, dispatch_group_wait is unblocked and tasks appended to dispatch_group_notify are executed

Example:

Create a queue group
dispatch_group_t group = dispatch_group_create();

// Create a queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

NSLog(@"Current thread --0--%@",[NSThread currentThread]);

// Put the task into the queue group
dispatch_group_enter(group);
dispatch_async(queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@"Task 1 - % @",[NSThread currentThread]);
    }
    
    // Take the task out of the queue group
    dispatch_group_leave(group);
});

// Put the task into the queue group
dispatch_group_enter(group);
dispatch_async(queue, ^{
    for (NSInteger i = 0; i<3; i++) {
        NSLog(@Task 2 - % @ "",[NSThread currentThread]);
    }
    
    // Take the task out of the queue group
    dispatch_group_leave(group);
});

/ / set dispatch_group_wait
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

// Block the current thread until all queues in this queue group are complete
NSLog(@"Current thread --3--%@",[NSThread currentThread]);
Copy the code

The log: