-GCD Queue and task understanding of Apple Multi-threaded network programming

Source: Internet
Author: User
Tags gcd

Today, the use of multi-threaded GCD IOS has been summed up and understood.

Gcd,grand Central Dispatch is a solution for multi-core programming developed by Apple.

GCD is actually the creation/acquisition of a queue, in block blocks or methods (functions) to write the task code block to execute, and then put the task in the appropriate queue to execute. GCD queue, divided into three kinds: Primary queue (serial queue), global queue (parallel queue), custom queue (custom). Task execution methods are divided into synchronous execution and asynchronous execution. As for serial queue, parallel queue, synchronous execution, asynchronous execution of the characteristics of self-learning, here do not do too much elaboration. probably means that.

// sync: Executes in the current thread and does not open a new thread

// async: A new thread is opened and the task executes in a new thread

// serial: Sequential, one-to-one execution (one execution is done before the next)

// Parallel: Multiple tasks are executed simultaneously (all tasks in the queue can be taken out, as long as the thread can be executed)

Here are three ways to get and create queues.

//1. Home Row/* Home column: Serial queue, via Dispatch_get_main_queue (); Get home row.    Serial queue Asynchronous execution: Execute Task1 First, then execute TASK2 */dispatch_queue_t Mainqueue = Dispatch_get_main_queue ();        Dispatch_async (Mainqueue, ^{for (int i = 0; i <; i + +) {NSLog (@ "Task1");        }        });        Dispatch_async (Mainqueue, ^{for (int i = 0; i <; i + +) {NSLog (@ "Task2"); }    });
2. Global queue:/    *     Global queue: Parallel queue. With Dispatch_get_global_queue (0, 0); get.     parameter 1: logo, arbitrarily set, general write 0.     parameter 2:4 global queues, with priority distinction. Generally also write 0     dispatch_queue_priority_high > Dispatch_queue_priority_default > Dispatch_queue_priority_low > Dispatch_queue_priority_background     parallel queue, asynchronous execution: Task1 and Task2 cross execution, 1-2-1-2 ...    *    /dispatch_queue_t globalqueen = dispatch_get_global_queue (0, 0);    Dispatch_async (Globalqueen, ^{for        (int i = 0; i <; i + +) {            NSLog (@ "Task1");        }            );        Dispatch_async (Globalqueen, ^{for        (int i = 0; i <; i + +) {            NSLog (@ "Task2");        }    );

3. Custom Queue    /*     custom queue, Need Dispatch_queue_create ("www.com", dispatch_queue_concurrent), to create     parameter 1: Identity, arbitrarily set string. Note: The GCD is encapsulated in the C language, so use "instead of @"     parameter 2: Set up a parallel or serial queue.         dispatch_queue_concurrent//Parallel Queue Asynchronous execution: Task1 and task2 random execution         dispatch_queue_serial//Serial Queue Asynchronous execution: Executes the Task1 first, then executes Task2    */    dispatch_queue_t queue1 = Dispatch_queue_create (" Www.com ", dispatch_queue_concurrent);    Dispatch_async (queue1, ^{for        (int i = 0; i <; i + +) {            NSLog (@ "Task1");        }    );    Dispatch_async (queue1, ^{for        (int i = 0; i <; i + +) {            NSLog (@ "Task2");        }    );


-GCD Queue and task understanding of Apple Multi-threaded network programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.