Good articles to my personal technology blog: https://cainluo.github.io/15018603425788.html


The introduction of the GCD

GCD stands for Grand Central Dispatch.

Grand Central Dispatch (GCD) is a newer solution to multi-core programming developed by Apple Dad. It is primarily used to optimize applications to support multicore processors and other symmetric multiprocessing systems. It is a parallel task that executes on a thread pool basis. First released on Mac OS X 10.6 Snow Leopard and also available on iOS 4 and above.

Why choose communist party CD

In iOS development, we all know that in addition to GCD, there are nsThreads, NSOperation, and NSOperationQueue, plus Pthreads, which are originally unix-based, but I won’t go into GCD here.

  • GCDIt can be used in multi-core parallel computing
  • GCDWill automatically use more CPU cores, there will not be a difficult core, nine cores around the situation.
  • GCDThe life cycle of threads is automatically managed, such as thread creation, task scheduling, thread destruction, and so on.
  • It’s just for us programmers, you knowGCDVery simple, just tell it what task to perform, management threads and so on.

Reprint statement: if you need to reprint this article, please contact the author, and indicate the source, and can not modify this article without authorization.


GCD tasks and queues

There are two core ideas to be popularized here: tasks and queues.

task

The so-called task refers to the operation that we programmers put in the GCD, which is generally carried out in Block mode. There are two kinds of operation to execute the task, synchronous execution and asynchronous execution. The difference between the two is whether to start a new thread for operation.

Synchronous execution: in the GCD is sync, no new thread will be opened, only the current thread will operate. Asynchronous execution: In GCD async, you can start a new thread to execute a task.

The queue

Queue queue refers to the task, used to store the task queue, queue is a kind of special linear table, is the first in first out (FIFO) principle, is like a highway toll, a start, from the front row in front of the car is pay the fee to go first, to get to the next, the queue is divided into two kinds, Parallel queues and serial queues.

Concurrent Dispatch Queue: Full name Concurrent Dispatch Queue, which refers to the ability to execute multiple tasks at the same time, if parallel queues are used, multiple threads are automatically enabled to execute tasks at the same time. Serial Dispatch Queue: Serial Dispatch Queue refers to the execution of tasks one after the other, just like the toll booth example.

Note: Parallel queues are only valid for asynchronous execution (dispatch_async).


How to use GCD

In fact, the use of GCD is very simple, two steps to complete.

  • Create queues, parallel, serialOK
  • The task is then placed in a queue and executed asynchronously or synchronously, depending on the type of task.

Let’s start by looking at how queues, tasks are created.

Create a queue

  • I can use it heredispatch_queue_createTo create the object, you need to pass in two parameters.
    • The first parameter: the unique identifier of the queue
    • Second parameter: the type of queue,DISPATCH_QUEUE_SERIALRepresents a serial queue,DISPATCH_QUEUE_CONCURRENTRepresents a parallel queue.
// Create a serial queue
dispatch_queue_t queue= dispatch_queue_create("Test.queue", DISPATCH_QUEUE_SERIAL);

// Create a parallel queue
dispatch_queue_t queue= dispatch_queue_create("Test.queue", DISPATCH_QUEUE_CONCURRENT);
Copy the code
  • We can also use parallel queuesdispatch_get_global_queueTo create aGlobal parallel queue, you also need to pass in two parameters.
    • The first parameter: the priority of the queueDISPATCH_QUEUE_PRIORITY_DEFAULT.
    • The second argument: it is not used yet, it can be used0To replace.

Create a task

// Synchronize the task creation method
dispatch_sync(queue, ^{
    // Put the task code here
});

// Execute the task creation method asynchronously
dispatch_async(queue, ^{
    // Put the task code here
});
Copy the code

Basically, if we’re done with this, we only have two queues here, but when combined, they’re quite a few:

  • Parallel queue + synchronous execution
  • Parallel queue + asynchronous execution
  • Serial queue + synchronous execution
  • Serial queue + asynchronous execution

Add in a special queue called the main queue, and you have two more combinations:

  • Main queue + synchronous execution
  • Main queue + asynchronous execution

Here we can look at a table to get a clearer picture of the differences between the combinations:

Parallel lines Serial queues The home side column
Sync Execution Do not start new threads, execute tasks in serial mode Do not start new threads, execute tasks in serial mode Do not start new threads, execute tasks in serial mode
Asynchronous execution Start a new thread to execute tasks in parallel Start a new thread to execute tasks in serial mode Do not start new threads, execute tasks in serial mode

conclusion

About the basic knowledge of GCD, here it is, more detailed, the follow-up article to fill up, or you can go to see the OFFICIAL DOCUMENTATION of GCD masturbation is ok~~


The last