GCD: Written in plain C, is the solution that Apple has proposed for parallel operations with multicore.
two core concepts of GCD: -Tasks -queues
< Span class= "token MD md-li" > add task to queue GCD will automatically take the task in the queue and put it in the corresponding thread execution The removal of the task follows the FIFO principle of the queue: first-in, LIFO, -create serial queue
// "WE" is the name of the thread // dispatch_queue_serial serial dispatch_queue_t QUEUE = dispatch_queue_create ("WE" , dispatch_queue_serial); // You can also set the serial default to nulldispatch_queue_t queue = dispatch_queue_create ("WE", NULL)
- Create a queue
// dispatch_queue_concurrent parallel dispatch_queue_t QUEUE = dispatch_queue_create ("WE" , dispatch_queue_concurrent);
-Asynchronous execution
Dispatch_async (Queue, ^{// });
-Synchronous execution
Dispatch_sync (Queue, ^{//});
Several usages of GCD -serial queue, synchronous execution-----Not thread, synchronous execution
dispatch_queue_t queue = dispatch_queue_create ("WE", dispatch_queue_serial); for (int05; + +i) {// synchronous Execution dispatch_sync (queue, ^ { NSLog (@ "%d--%@", I,[nsthread CurrentThread]);} );
-Serial queue, asynchronous execution-----Open a thread, execute sequentially
dispatch_queue_t queue = dispatch_queue_create ("WE", dispatch_queue_serial); for (int05; + +i) {// Async Execute dispatch_async (queue, ^ { NSLog (@ "%d--%@", I,[nsthread CurrentThread]);} );
-Parallel queue, asynchronous execution----- open multiple threads, execute asynchronously
= Dispatch_queue_create ("WE", dispatch_queue_concurrent); for (int0; + +i) {// // Asynchronous Execution Dispatch_async (Queue, ^{ NSLog (@ " async >%d--%@", i,[ Nsthread CurrentThread]);} );
-Parallel queue, synchronous execution-----Not thread, sequential execution
dispatch_queue_t Queue= Dispatch_queue_create ("WE", dispatch_queue_concurrent); for(inti =0; I <Ten; ++i) {//Synchronous ExecutionDispatch_sync (Queue, ^{NSLog (@"Sync >%d--%@", I,[nsthread CurrentThread]); }); }
-Host column, asynchronous task-----Not thread, synchronous execution
dispatch_queue_t queue = dispatch_get_main_queue (); for (int05; + +i) {//// Asynchronous Execution Dispatch_async (Queue, ^{ NSLog (@ "%d--%@", I,[nsthread CurrentThread]);} );
-The home row, synchronous execution-----program does not perform (deadlock) The cause of the deadlock, when the program executes to the following code
1) Home column: Do not dispatch a task if the main thread is executing code
2) Synchronous execution: If the first task is not executed, continue to wait for the first task to complete, then perform the next task, at which time each other waits and the program cannot execute (deadlock)
-Global queue (essentially a concurrent queue)
// first parameter: IOS7 and iOS8 This parameter can be passed directly to the 0 Span style= "color: #008000;" >// second argument: Always pass in 0 dispatch_queue_t queue = Dispatch_get_global_ for future use Queue (0 , 0 ); for (int i = 0 ; I < 5 ; ++i) { Parallel execution dispatch_async (queue, ^{NSLog ( @ " %d--%@ "
IOS GCD Multithreading Introduction