IOS GCD and iOSGCD

Source: Internet
Author: User

IOS GCD and iOSGCD
GCD Grand Central Dispatch written in pure C Language

1. GCD is Apple's solution for multi-core parallel operations

GCD will automatically use more CPU cores (such as dual-core and quad-core)

GCD automatically manages the thread lifecycle (creating threads, scheduling tasks, and destroying threads)

The programmer only needs to tell GCD what task to execute and does not need to write any thread Management Code.

Process: A running application.

Thread: a complete execution path in the process. A process can have multiple threads, at least one of which is the main thread. In iOS development, all UI interfaces must be updated in the main thread.

2. Working Principle of GCD: Let the program queue in parallel and arrange them to execute tasks on any available processor based on available processing resources

3. Serial queue, parallel queue, synchronization task, asynchronous task. Asynchronous tasks will open up threads: asynchronous tasks enable only one sub-thread in the serial queue, while asynchronous tasks enable multiple sub-threads in the parallel queue.

// GCD parallel queue, asynchronous task: multiple threads are enabled and executed simultaneously. Dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async (queue, ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "AAAAA % d", I) ;}}); dispatch_async (queue, ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "BBBBB % d", I) ;}}); dispatch_async (queue, ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "CCCCC % d", I) ;}}); // serial queue, synchronization task: thread disabled, execute dispatch_queue_t Cqueue = dispatch_queue_create ("queueName", NULL); dispatch_sync (Cqueue, ^ {for (int I = 0; I <1000; I ++) in sequence) {NSLog (@ "DDDDD % d", I) ;}}); dispatch_sync (Cqueue, ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "EEEEE % d", I) ;}}); dispatch_sync (Cqueue, ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "FFFFF % d", I );}});

 

 

4. We usually use multithreading and often encounter a requirement that the main thread needs to update the UI after the sub-thread task is processed. How can we know that all the sub-thread tasks have been completed.

When we use a serial queue, we only need to add the last callback main thread task to the end of all tasks for sequential execution. However, when we use parallel queues, we cannot know when to complete all subthread tasks,

At this time, we need to use dispatch_group. The Code is as follows:

// We can execute several asynchronous tasks at the same time. If they are finished, we sometimes have to know what to do. At this time, we have to use dispatch_group, as shown in the code below, run dispatch_group_notify at the end, and then dispatch_queue_t queue = queue (queue, 0); dispatch_group_t group = dispatch_group_create (); dispatch_group_async (group, queue, ^ {NSLog (@ "start execution 1") ;}); dispatch_group_async (group, queue, ^ {NSLog (@ "start execution 2") ;}); dispatch_group_async (group, queue, ^ {NS Log (@ "start execution 3") ;}); dispatch_group_y y (group, queue, ^ {NSLog (@ "All execution is complete and the UI must be updated in the main thread !!! "); Dispatch_async (dispatch_get_main_queue (), ^ {NSLog (@" the main thread has finished updating the UI. ");});});

 

* If any of the above information is incorrect, you are welcome to make corrections.

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.