What is GCD: (Grand [Great] central [center] dispatch[Dispatch])
GCD also known as "the Great Central dispatcher", he was iOS4 after the introduction of a multithreaded technology. The developer simply defines the task to be executed to append to the dispatch queue. GCD can generate the necessary threads and perform scheduled tasks. The essence of GCD is actually the queue.
Advantages of two GCD
The emergence of 1.GCD solves the problem of multi-core parallel operation
2.GCD automatically manages the life cycle of threads
3. Simply describe the task without writing any thread-managed code
Note: The dispatch queue generated by the Dispatch_queue_create method does not automatically manage memory by arc. You can use Dispatch_release, Dispatch_retain to manually manage (reference counting), but for the moment, the OSX-10.8 opened arc is no longer required to be managed by Dispatch_release ().
Three GCD of the following rules
The removal of a task in the GCD follows the FIFO principle of the queue : first-in, LIFO, and backward.
Four GCD how to perform a task
Create a serial queue
dispatch_queue_t queue = dispatch_queue_create("First" , dispatch_queue_serial); Dispatch_queue_serial is default to NULL, which is the identifier of the serial queue
1. Serial Queue synchronous Execution: dispatch_sync(queue, ^{ NSLog(@ "1"); }); Keyword sync means sync
Serial queue Synchronous Execution: does not open threads and executes sequentially (i.e. it consumes memory and does not speed up the program, essentially meaningless).
2. Serial queue Asynchronous execution:dispatch_async(queue, ^{ NSLog(@ "1"); keyword async means async
Serial queuing executes asynchronously: threads are opened, tasks within the queue are progressively executed, and tasks within the thread execute concurrently with tasks outside the thread (useful).
Create a parallel queue
dispatch_queue_t queue = dispatch_queue_create("Second" , dispatch_queue_serial) ; Dispatch_queue_serial is a flag for parallel queues
3. Parallel queue execution:dispatch_sync(queue, ^{ NSLog(@ "1"); keyword Sync indicates synchronization
Parallel queue Synchronous Execution: does not open threads and executes in order. (basically no use).
4. Parallel queue Asynchronous execution: Dispatch_async(queue, ^{ NSLog(@ "1"); keyword async means async
Parallel queues Execute asynchronously: Multiple threads are opened, and the number of threads is determined by the GCD bottom. Tasks on columns are performed synchronously (unable to determine the order in which tasks are executed in the queue, error-prone).
Summary: A queue that is executed synchronously does not open threads and asynchronously opens threads
Four. Two special queues in the GCD
1. Home column: The tasks that are placed in the main queue are executed on the main thread.
Use Dispatch_get_main_queue () to get the home row
dispatch_queue_t queue = Dispatch_get_main_queue ();
There is only one main thread for each application, that is, there is only one home column, and the operations in the main queue are performed in the main thread, and there is no concept of asynchrony.
Note: Synchronization operations added in the primary queue will never be executed (deadlock)
2. Generation of global queues
Ispatch_async (Dispatch_get_global_queue (0, 0), ^{})
Global queue: The global queue is a system that can be used directly, similar to a parallel queue, but cannot specify the name of the queue, and cannot be confirmed at debug time.
Perform synchronization tasks in the global queue: Do not create new threads, perform tasks sequentially
Perform an asynchronous task in a global queue: Multiple threads are created, but the order in which the tasks are executed cannot be determined
3. Priority of global concurrent queues:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2//High
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0//default (Medium)
#define Dispatch_queue_priority_low (-2)//Low
#define Dispatch_queue_priority_background int16_min//Backstage
DEMO:
See next article.
Local parsing of iOS GCD