IOS-GCD Use detailed
Objective
For beginners, GCD seems to be a barrier to the past, many people in the synchronous, asynchronous, serial, parallel and deadlock in the vortex of several nouns gradually abandoned treatment. This article will use the graphic form and Mao's way to give you an image of the principles and rules.
Concepts of threads, tasks, and queues
Asynchronous, synchronous & parallel, serial features
An important rule.
In general, the biggest goal of using GCD is to execute multiple tasks simultaneously in a new thread, which means we need two conditions:
Can open a new thread
Tasks can be executed concurrently
Combined with the above two conditions, it is also equivalent "the ability to open a new thread + the right to execute the task synchronously", only in the context of the ability and rights to meet the two conditions, we can perform multiple tasks at the same time.
The characteristics of all combinations
(i) asynchronous execution + parallel queues
Implementation code:
Asynchronous execution + Parallel queue
-(void) asyncconcurrent{
//Create a parallel queue
dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_concurrent);
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 results:
1 2 3 4 5 |
---start--- ---end--- 任务3---{number = 5, name = (null)} 任务2---{number = 4, name = (null)} 任务1---{number = 3, name = (null)} |
Explain
Step diagram
(ii) asynchronous execution + serial queue
Implementation code:
Asynchronous execution + Serial queue
-(void) asyncserial{
//Create a serial queue
dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_serial);
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 results:
1 2 3 4 5 |
---start--- ---end--- 任务1---{number = 3, name = (null)} 任务2---{number = 3, name = (null)} 任务3---{number = 3, name = (null)} |
Explain
Step diagram
(iii) synchronous execution + parallel queues
Implementation code:
Synchronous execution + Parallel queue
-(void) syncconcurrent{
//Create a parallel queue
dispatch_queue_t \ dispatch_queue_create ("identifier", dispatch_queue_concurrent);
NSLog (@ "---start---");
Encapsulates three task
Dispatch_sync (the queue, ^{
NSLog (@ Task 1---%@, [nsthread CurrentThread]) using
a synchronization function;
Dispatch_sync (queue, ^{
NSLog (@ "Task 2---%@", [Nsthread CurrentThread]);
Dispatch_sync (queue, ^{
NSLog (@ "Task 3---%@", [Nsthread CurrentThread]);
NSLog (@ "---end---");
}
Print results:
1 2 3 4 5 |
---start--- 任务1---{number = 1, name = main} 任务2---{number = 1, name = main} 任务3---{number = 1, name = main} ---end--- |
Explain
Step diagram
(iv) synchronous execution + serial queue
Implementation code:
-(void) syncserial{
//Create a serial queue
dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_serial) ;
NSLog (@ "---start---");
Use asynchronous functions 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 results:
1 2 3 4 5 |
---start--- 任务1---{number = 1, name = main} 任务2---{number = 1, name = main} 任务3---{number = 1, name = main} ---end--- |
Explain
The implementation principle and step diagram here is the same as "synchronous execution + concurrent queue", so as long as it is executed synchronously, it is impossible to open new threads, so the multiple tasks can be executed in sequence only.
(v) Asynchronous execution + home team column
Implementation code:
-(void) asyncmain{
//Get home team column
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 results:
1 2 3 4 5 |
---start--- ---end--- 任务1---{number = 1, name = main} 任务2---{number = 1, name = main} 任务3---{number = 1, name = main} |
Explain
Asynchronous execution means that the
The difference between a master queue and a serial queue
The above conditions are combined to produce results:
Step diagram
(vi) Synchronous execution + home row (deadlock)
Implementation code:
-(void) syncmain{
//Get home team column
dispatch_queue_t queue = Dispatch_get_main_queue ();
NSLog (@ "---start---");
Encapsulates three task
Dispatch_sync (the queue, ^{
NSLog (@ Task 1---%@, [nsthread CurrentThread]) using
a synchronization function;
Dispatch_sync (queue, ^{
NSLog (@ "Task 2---%@", [Nsthread CurrentThread]);
Dispatch_sync (queue, ^{
NSLog (@ "Task 3---%@", [Nsthread CurrentThread]);
NSLog (@ "---end---");
}
Print results:
Explain
Tasks in the master queue must be executed sequentially
Task 1 to execute when the main thread is free (that is, all the tasks in the main queue are finished)
The main thread will not be empty until the "Print End" task is performed
Task 1 and Print end two tasks wait on each other, causing deadlocks
Step diagram
It's written at the end.
The above is my understanding of the basic knowledge and several combinations of GCD.