Basic Concept ***********************************
1. Grand Central Dispatch (GCD) is a newer approach to multi-core programming developed by Apple and is the multi-threaded processing mechanism of Apple's main push. In the multi-core CPU state, the GCD performance is very high.
It automatically leverages more CPU cores to manage the thread life cycle, and programmers don't need to write any thread-management code, just a given task for GCD to perform.
2. GCD is a pure C language, and most of the functions in GCD begin with dispatch .
3. GCD exists in the Libdispatch.dylib Library, but does not need to be imported manually, which is included by default.
4. Two core concepts of GCD: tasks and queues
Adding tasks to the queue, GCD automatically takes the tasks in the queue, executes them in the corresponding thread, and follows the FIFO principle: first-in, LIFO, and backward-out.
5. GCD is generally used in conjunction with block to handle program operations in block callbacks.
GCD Three scheduling queues ******************************
The concept of 4 terms needs to be clarified first:
1. Asynchronous (Async): Executed on another line with the ability to open a new thread
2. Sync: Execute on current line, no ability to open new thread
3. Parallel (concurrent): Automatically open multiple threads for simultaneous execution of multiple tasks, parallel functionality only works under asynchronous conditions
4. serial (serial): One task execution completes before executing the next
In summary : synchronous and asynchronous decide whether to open a new thread, parallel and serial determine how the task executes.
Three scheduling queues for GCD:
1. The main queue that runs on the main thread is typically the execution and UI-related tasks such as updating the UI display, obtained through Dispatch_get_main_queue.
2. Concurrent queue global dispatch queue, which is typically a long-run task in the background such as downloads, provided by default, does not need to be created, obtained through Dispatch_get_global_queue.
1dispatch_queue_t Dispatch_get_global_queue (dispatch_queue_priority_t priority,unsignedLongflags)2 //dislatch_queue_t indicates that a queue is returned3 /*the first parameter dispatch_queue_priority_t priority, followed by the default priority4 Dispatch_queue_priority_default*/5 //The second parameter, unsigned long, is used later, so first pass 0.6 7 //usage8dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);9 //get the global concurrent queues queue
3. Serial queue serial queues, there are two ways to get serial
1) Create a serial queue using the Dispatch_queue_create function
1dispatch_queue_t Dispatch_queue_create (Const Char*label, dispatch_queue_attr_t attr)2 //The first parameter is the queue name, the second parameter is the queue property, and is generally null3 4 //usage5dispatch_queue_t queue = Dispatch_queue_create ("Blahblahblah", NULL);6 7 //Non-ARC requires manual release of queue8 //dispatch_release (queue);
2) Use the primary queue (a special serial queue that gcd the queues that are executed in the main thread)
1 Dispatch_get_main_queue () 2 3 // usage 4 dispatch_queue_t queue = Dispatch_get_main_queue ();
Code Sample **********************************
1. Adding tasks to concurrent queues with asynchronous functions
//VIEWCONTROLLER.M #import<ViewController.h>@interfaceViewcontroller ()@end@implementationViewcontroller-(void) viewdidload{[Super Viewdidload]; //1. Get Global concurrency Queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);//2. Adding tasks to the queueDispatch_async (queue,^{NSLog (@"Download Image 1----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue,^{NSLog (@"Download Image 2----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue,^{NSLog (@"Download Image 3----%@", [Nsthread CurrentThread]); }); NSLog (@"main thread----%@", [Nsthread mainthread]);}@end//asynchronous, concurrent, 3 child threads turned on
2. Adding tasks to a serial queue with an asynchronous function
//VIEWCONTROLLER.M#import<ViewController.h>@interfaceViewcontroller ()@end@implementationViewcontroller-(void) viewdidload{
[Super Viewdidload]; NSLog (@"main thread----%@", [Nsthread mainthread]); dispatch_queue_t Queue= Diapatch_queue_create ("Blahblahblah", NULL); Dispatch_async (Queue,^{NSLog (@"Download Image 1----%@", [Nsthread CurrentThread])}); Dispatch_async (Queue,^{NSLog ("Download Image 2----%@", [Nsthread CurrentThread]); }) Diapatch_async (queue,^{NSLog ("Download Image 3----%@", [Nsthread CurrentThread]); }) //Release Queue//dispatch_release (queue);)//asynchronous, serial, with only one thread open, sub-thread's task serially executed
3. Adding a task to a concurrent queue with a synchronization function
1 //VIEWCONTROLLER.M2 3 #import<ViewController.h>4 5 @interfaceViewcontroller ()6 7 @end8 9 @implementationViewcontrollerTen-(void) viewdidload{ One [Super Viewdidload]; ANSLog (@"main thread----%@", [Nsthread mainthread]); -dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); -Dispatch_sync (queue,^{ theNSLog ("Download Image 1----%@", [Nsthread CurrentThread]); - }) -Dispatch_sync (queue,^{ -NSLog ("Download Image 2----%@", [Nsthread CurrentThread]); + }) -Dispatch_sync (queue,^{ +NSLog ("Download Image 3----%@", [Nsthread CurrentThread]); A }) at - ) - - //synchronous, concurrent, concurrent queue loses concurrency, does not have the ability to open new threads, only the main thread
4. Add a task to the serial queue with a synchronization function
1 //VIEWCONTROLLER.M2 3 #import<ViewController.h>4 5 @interfaceViewcontroller ()6 7 @end8 9 @implementationViewcontrollerTen-(void) viewdidload{ One [Super Viewdidload]; ANSLog (@"main thread----%@", [Nsthread mainthread]); -dispatch_queue_t queue = Dispatch_queue_create ("Blahblahblah", NULL); -Dispatch_sync (queue,^{ theNSLog (@"Download Image 1----%@", [Nsthrea CurrentThread]); - }) -Dispatch_sync (queue,^{ -NSLog (@"Download Image 2----%@", [Nsthrea CurrentThread]); + }) -Dispatch_sync (queue,^{ +NSLog (@"Download Image 3----%@", [Nsthrea CurrentThread]); A }) at - ) - @end - - //synchronization, serial, no ability to open new threads, only the main thread
Summarize:
Synchronization functions
(1) Concurrent queue: no thread
(2) Serial queue: no thread
Asynchronous functions
(1) Concurrent queue: can open n threads
(2) Serial queue: Open 1 threads
Multithreading-GCD Learning Notes