ios GCD

Keywords: Mobile

1. Benefits of GCD:

  • GCD can be used for multi-core parallel operations
  • Will automatically take advantage of more CPU kernels
  • GCD automatically manages the lifecycle of threads (creating threads, scheduling tasks, destroying threads)
  • Programmers need only tell the GCD what tasks they want to perform, and they don't need any thread management code.
    2. Tasks and queues
    Task: Perform operations
  • Synchronous and asynchronous execution
    Differentiation: Whether to wait for the queue task to finish execution, and whether to have the ability to open new threads.
    Synchronized execution: Only in the current thread to execute tasks, do not have the ability to open new threads.
    Asynchronous execution:
    Asynchronous addition of tasks to the specified queue, he will not do any waiting, can continue to execute tasks, can execute tasks in new threads, with the ability to open new threads.
    Be careful:
    Although asynchronous execution has the ability to open new threads, it does not necessarily open new threads.
  • Serial and concurrent queues:
    The execution order and the number of open threads are different.
    Serial: Only one task is executed at a time, so that tasks are executed one after another. (Only one thread is opened, one task after another is executed)
    Concurrent: Multiple threads can be opened and tasks can be executed simultaneously.
    (Concurrent function of concurrent queue is only valid under asynchronous function)
    Use steps:
    1. Create a queue (serial or parallel)
    2. Append the task to the waiting queue of the task, and the system will execute the task (synchronous or asynchronous) according to the type of task.
    DISPATCH_QUEUE_SERIAL denotes a serial queue and DISPATCH_QUEUE_CONCURRENT denotes a concurrent queue.
dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL);
// Method of creating concurrent queues
dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);

6 combinations

Communication between GCD s
Execute tasks in other threads first, then return to the main thread to perform the corresponding operations of the main thread.

- (void)communication {
    // Getting global concurrent queues
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    // Get the main queue
    dispatch_queue_t mainQueue = dispatch_get_main_queue(); 
    
    dispatch_async(queue, ^{
        // Asynchronous Additional Tasks
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // Simulated time-consuming operation
            NSLog(@"1---%@",[NSThread currentThread]);      // Print the current thread
        }
        
        // Back to the main thread
        dispatch_async(mainQueue, ^{
            // Tasks appended to the main thread
            [NSThread sleepForTimeInterval:2];              // Simulated time-consuming operation
            NSLog(@"2---%@",[NSThread currentThread]);      // Print the current thread
        });
    });
}

deadlock

  • Adding synchronous tasks to the task queue of the current thread causes deadlocks
- (void)syncMain{
    NSLog(@"currentThread---%@", [NSThread currentThread]);
    NSLog(@"syncMain---begin");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        for(int i = 0;i < 2;i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for ( int i = 0; i < 2; i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for(int i = 0;i < 2;i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    
}

//Threads are used to execute tasks, and queues are used to store tasks waiting to be executed.
//A main thread, in which the main queue is in, dispatch_sync is a task, waiting for the return value in the block to end;  
//Tasks (blocks) added to the main queue also wait for tasks in the current thread to complete... Waiting for each other causes deadlock.

GCD queue group

- (void)groupNotify{
    NSLog(@"currentThread---%@", [NSThread currentThread]);
    NSLog(@"group---begin");
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for(int i = 0; i < 2; i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1----%@", [NSThread currentThread]);
        }
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for(int i = 0; i < 2; i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        for(int i = 0;i < 2;i++){
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@", [NSThread currentThread]);
        }
    });
    //All tasks, such as additional tasks, will not be executed until they have been completed.
}

Posted by porko2004 on Tue, 29 Jan 2019 01:57:15 -0800