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
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
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
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
What is the difference between the main queue and the serial queue?
The result is obtained by combining the preceding conditions:
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:
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.