Brief introduction
GCD itself is the solution that Apple has proposed for multi-core parallel computing. GCD will automatically utilize more processor cores at work to take advantage of more powerful machines. GCD is the abbreviation for Grand Central Dispatch, which is based on the C language. If you use GCD, the thread is completely managed by the system, we do not need to write thread code. Simply define the tasks you want to perform and add them to the appropriate dispatch queue (dispatch queue). GCD is responsible for creating threads and dispatching your tasks, and the system directly provides thread management
GCD advantages
GCD can be used for multi-core parallel operations
GCD will automatically take advantage of more CPU cores (such as dual-core, quad-core)
GCD automatically manages the life cycle of threads (create threads, schedule tasks, destroy threads)
Programmers just need to tell gcd what tasks they want to perform, without having to write any thread management code
Tasks and queues
Two core concepts in GCD
Task: What to do, in GCD is a block, the two ways of task execution: Synchronous execution and asynchronous execution. The main difference is whether you have the ability to open new threads
Synchronous Execution (Sync): Tasks can only be performed in the current thread, without the ability to open new threads
Asynchronous Execution (Async): A task can be executed in a new thread with the ability to open a new thread
Queue: A queue is a special linear table with a FIFO (first-in-one-out) principle, in which new tasks are always inserted at the end of the queue, while read tasks are always read from the head of the queue. For each task read, a task is freed from the queue. There are two types of queues in GCD: Serial queue and parallel queue.
Attention:
Tasks that are placed in parallel queues, GCD also
FIFO
, but the difference is that it takes one out and puts it on another thread, and then it pulls out another thread. As a result of taking the action quickly, negligible, it seems that all the tasks are executed together. However, it is important to note that GCD will control the number of parallelism based on system resources, so if there are many tasks, it will not allow all tasks to execute concurrently.
Parallel queues (Concurrent Dispatch queue): Multiple tasks can be executed in parallel (simultaneously) (automatic opening of multiple threads concurrently executing tasks)
Parallel functions are only valid under asynchronous (Dispatch_async) functions
Serial Queue (Serial Dispatch queue): Let the task execute one after the other (after a task is completed, the next task is performed)
GCD Steps to use
1. Create a queue (serial queue or parallel queue)
2. Add the task to the queue and the system will perform the task according to the task type (synchronous execution or asynchronous execution)
1. How to create a queue
You can use dispatch_queue_create
to create an object, you need to pass in two parameters, the first parameter represents a unique identifier for the queue, is used for debug, can be null, and the second parameter is used to identify whether it is a serial queue or a parallel queue. DISPATCH_QUEUE_SERIAL
represents a serial queue, which DISPATCH_QUEUE_CONCURRENT
represents a parallel queue.
// 串行队列的创建方法
dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);
// 并行队列的创建方法
dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
For parallel queues, you can also use dispatch_get_global_queue
to create global parallel queues. GCD provides a global parallel queue by default and requires two parameters to be passed in. The first parameter indicates a queue priority, which is generally used DISPATCH_QUEUE_PRIORITY_DEFAULT
. The second parameter is temporarily useless and 0
can be used.
2. How to create a task
// 同步执行任务创建方法dispatch_sync(queue, ^{ NSLog(@"%@",[NSThread currentThread]); // 这里放任务代码});// 异步执行任务创建方法dispatch_async(queue, ^{ NSLog(@"%@",[NSThread currentThread]); // 这里放任务代码});
The GCD of multithreading development