IOS-GCD usage and iostag usage

Source: Internet
Author: User

IOS-GCD usage and iostag usage

Preface

For beginners, GCD seems to be an insurmountable hurdle. Many people gradually give up treatment in the whirlpool of terms of synchronization, Asynchronization, serial, parallel and deadlock. This article will explain the principles and rules in a graphic and illustrated manner.

 

Concepts of threads, tasks, and queues

Asynchronous, synchronous, parallel, and serial features

An important criterion

In general, the maximum purpose of using GCD is to execute multiple tasks simultaneously in the new thread, which means we need two conditions:

 

  • New Threads can be opened

  • The task can be executed simultaneously.

  • Combining the above two conditions is equivalent to "the ability to enable new threads + the right to execute tasks synchronously". Only when the two conditions of capability and right are met, we can execute multiple tasks at the same time.

 

Features of all combinations

(1) asynchronous execution + parallel queue

Implementation Code:

// Asynchronous execution + parallel queue-(void) asyncConcurrent {// create a parallel queue dispatch_queue_t queue = dispatch_queue_create ("identifier", DISPATCH_QUEUE_CONCURRENT ); NSLog (@ "--- start ---"); // use an asynchronous function to encapsulate three tasks: dispatch_async (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]) ;}); NSLog (@ "--- end ---");}

 

Print result:

12345 ---start---  ---end---  Task 3 --- {number = 5, name = (null )}  Task 2 --- {number = 4, name = (null )}  Task 1 --- {number = 3, name = (null )}

Explanation

 

    • Asynchronous execution means

      • New Threads can be enabled.

      • The task can be bypassed before execution.

    • Parallel queue means

      • Tasks do not need to be queued and have the "right" to be executed at the same time"

    • Result After combination of the two

      • Three new threads are enabled.

      • When the function is executed, start and end are printed first, and then the three tasks are executed later.

      • The three tasks are executed at the same time, so the printed result is "Task 3 --> Task 2 --> Task 1"

 

Steps

(2) asynchronous execution + Serial queue

Implementation Code:

// Asynchronous execution + Serial queue-(void) asyncSerial {// create a serial queue dispatch_queue_t queue = dispatch_queue_create ("identifier", DISPATCH_QUEUE_SERIAL ); NSLog (@ "--- start ---"); // use an asynchronous function to encapsulate three tasks: dispatch_async (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]) ;}); NSLog (@ "--- end ---");}

 


 

Print result:

12345  ---start--- ---end---Task 1 --- {number = 3, name = (null )}Task 2 --- {number = 3, name = (null )}Task 3 --- {number = 3, name = (null )}

 

Explanation

  • Asynchronous execution means

    • New Threads can be enabled.

    • The task can be bypassed before execution.

  • Serial queue means

    • Tasks must be executed one by one in the order they are added to the queue.

  • Result After combination of the two

    • A new sub-thread is opened.

    • When the function is executed, start and end are printed first, and then the three tasks are executed later.

    • These three tasks are executed in order, so the printed result is "Task 1 --> Task 2 --> Task 3"

Steps

(3) Synchronous execution + parallel queue

Implementation Code:

// Synchronous execution + parallel queue-(void) syncConcurrent {// create a parallel queue dispatch_queue_t queue = dispatch_queue_create ("identifier", DISPATCH_QUEUE_CONCURRENT ); NSLog (@ "--- start ---"); // use the synchronous function to encapsulate three tasks dispatch_sync (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]) ;}); NSLog (@ "--- end ---");}

 


Print result:

12345 ---start---  Task 1 --- {number = 1, name = main}  Task 2 --- {number = 1, name = main}  Task 3 --- {number = 1, name = main}  ---end---

Explanation

  • Synchronous execution means

    • New threads cannot be enabled.

    • After a task is created, it must be executed before it can proceed.

  • Parallel queue means

    • Tasks must be executed one by one in the order they are added to the queue.

  • Result After combination of the two

    • All tasks can only be executed in the main thread.

    • When a function is executed, it must be executed in one row in the order of writing the code to continue.

  • Notes

    • In this case, even in parallel queues, tasks can be executed at the same time, but because there is only one main thread, it is impossible to distribute tasks to different threads for synchronous processing, the result is that it can only be executed in sequence in the main thread.

Steps

(4) Synchronous execution + Serial queue

Implementation Code:

-(Void) syncSerial {// create a serial queue dispatch_queue_t queue = dispatch_queue_create ("identifier", DISPATCH_QUEUE_SERIAL); NSLog (@ "--- start ---"); // use an asynchronous function to encapsulate three tasks: dispatch_sync (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]);}); NSLog (@ "--- end ---");}

 


 

Print result:

12345   ---start---  Task 1 --- {number = 1, name = main}  Task 2 --- {number = 1, name = main}  Task 3 --- {number = 1, name = main}  ---end---

Explanation

  • The execution principle and step diagram here are the same as those of "synchronous execution + concurrent queue". As long as it is synchronous execution, new threads cannot be started, therefore, multiple tasks can only be executed in order,

(5) asynchronous execution + main queue

Implementation Code:

-(Void) asyncMain {// obtain the dispatch_queue_t queue = dispatch_get_main_queue (); NSLog (@ "--- start ---"); // use Asynchronous functions to encapsulate three tasks: dispatch_async (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]);}); NSLog (@ "--- end ---");}

 

Print result:

12345   ---start---  ---end---  Task 1 --- {number = 1, name = main}  Task 2 --- {number = 1, name = main}  Task 3 --- {number = 1, name = main}

 

Explanation

  • Asynchronous execution means

    • New Threads can be enabled.

    • The task can be bypassed before execution.

  • What is the difference between the main queue and the serial queue?

    • Tasks in the queue must be executed in the same order.

    • Tasks in the main queue column must be executed in the main thread and cannot be executed in the Child thread.

  • The result is obtained by combining the preceding conditions:

    • All tasks can be skipped and then executed in order.

Steps

(6) Synchronous execution + main queue (deadlock)

Implementation Code:

-(Void) syncMain {// obtain the dispatch_queue_t queue = dispatch_get_main_queue (); NSLog (@ "--- start ---"); // use the synchronization function to encapsulate three tasks: dispatch_sync (queue, ^ {NSLog (@ "Task 1 --- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "Task 2 --- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "Task 3 --- % @", [NSThread currentThread]);}); NSLog (@ "--- end ---");}

 


Print result:

1   ---start---

Explanation

  • Tasks in the main queue column must be executed in sequence.

  • Task 1 can be executed only when the main thread is free (that is, all tasks in the main queue are completed ).

  • The main thread is free only after the "Print end" task is executed.

  • "Task 1" and "Print end" wait for each other, causing a deadlock

Steps

 

Written at the end

The above is my understanding of the basic knowledge and several combinations of GCD. If you think that my blog can be written well, please pay attention to my blog, I will launch a high-quality technical blog for you for a long time. Of course, if you think I have any mistakes, you can leave your comments.

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.