This article copies, references from the article: iOS multithreaded Programming Grand Central Dispatch (GCD) introduced and used, mainly in order to strengthen the understanding of the knowledge and memory of the individual, do not do his use. Original Author statement:
Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!
Thanks for the hard work of the original author here!
1. Introduction
The GCD (Grand Central Dispatch) is built on the thread pool pattern of parallel execution of tasks to optimize support for multi-core, multiprocessor systems.
GCD works by allowing programs to queue in parallel to specific tasks, scheduling them to perform tasks on any available core, depending on the available processing resources. A task can be a function or a block.
The bottom of the GCD is still threaded, but it doesn't have to be concerned with implementation details.
The FIFO queue of the GCD is called the Dispatch queue (dispatch), which guarantees first-come execution first. Dispatch, the queue is divided into the following 3 kinds:
(1) Serial (continuous)
Also known as the private dispatch queue (Pirvate-Dispatch queue), while performing only one task. Serial queues are typically used to synchronize access to specific resources or data. When more than one serial queue is created, the queues are executed concurrently, although they are executed separately within their respective queues.
(2) Concurrent (concurrent)
Also known as the global dispatch queue, multiple tasks in the queue can be executed concurrently, but the order in which the execution is completed is random.
(3) Main dispatch queue
It is a globally available serial queue that performs tasks on the main thread of the application.
2. Use of scheduling queues
2.1 Common methods of Dispatch_async
To prevent the interface from being stuck in processing time-consuming operations, such as reading network data, IO, database reading and writing, we will process these operations in another thread and then notify the main thread to update the interface.
Using GCD to implement this process is simpler than the Nsthread\nsoperation method described previously, and the code is as follows:
0), ^{ // Time-consuming operation ^{ // Update interface });});
Example:
IOS Multithreaded Learning Note--GCD