This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

review

In the last two posts, we’ve covered processes and threads, and we’ve also covered spinlocks and mutex. The most popular feature in iOS development is the GCD, so we’ll start with this post.

Multithreading in iOS (part 1) – Processes and threads

Multithreading in iOS (Part 2) – Threads and locks

1. What is the COMMUNIST Party

The GCD definition

Grand Center Dispatch, or GCD, is a technology developed by Apple to optimize applications to support multi-core processors.

  • pureCLanguage, which provides a lot of expected functions.
  • GCDIt’s apple’s solution for multi-core parallel computing.
  • GCDWill automatically use moreCPUThe core (e.g., dual-core, quad-core),GCDThread lifecycles are automatically managed (creating threads, scheduling tasks, destroying threads)
  • The programmer just has to tellGCDYou don’t have to write any thread management code to do what you want.

GCD adds the task to the queue and specifies the function to execute the task

How the GCD works

The two cores of the GCD: tasks and queues

2. Tasks and queues

Task: What action needs to be performed queue: to hold the task

GCD uses two steps: create the task and add the task to the queue

  • Create tasks: Determine what to do
  • Add a task to a queue
    • GCDTasks in the queue will be automatically taken out and put into the corresponding thread to execute.
    • The removal of tasks follows FIFOPrinciple: first in, first out, last in, last out.

Serial queues

Serial Dispatch Queue: Tasks are executed one after another. After one task is executed, the next task is executed. Serial Queue is sequential execution.

  • Schematic diagram of serial queue:

Concurrent queue

A Concurrent Dispatch Queue allows multiple tasks to execute concurrently and automatically enables multiple threads to execute tasks simultaneously.

The concurrent function is only valid for the asynchronous (dispatch_async) function.

  • Schematic diagram of concurrent queues:

Concurrent queues, the ones that are scheduled to execute first don’t have to execute first, which is the same asCPUIt has to do with the allotted time, it has to do with the size of the task, and if you don’t finish it in the allotted time,CPU: Old iron you first stand aside will, let the next brother to execute first, next time allocated to your time to continue to execute it.

3. The function

  • Task usingblockencapsulation
  • The task ofblockThere are no arguments and no return values
  • The function that performs the task

asynchronousdispatch_async

  • The next statement can be executed without waiting for the current statement to complete
  • Thread execution is enabledblockThe task of
  • Asynchronous is synonymous with multithreading

synchronousdispatch_sync

  • The next statement is executed only after the current statement is completed
  • Will not start the thread
  • In current executionblockThe task of

(1) Have specific tasks queued in parallel by the program and schedule them to perform on any available processor core based on available processing resources.

(2) A task can be a function, which is a closure body. The underlying meaning of the GCD is that it is implemented using threads, but it frees the programmer from paying attention to the implementation details.

(3) the GCD is must be used in a variety of queue, we met block, the specific code in the queue, the queue queue task execution, the system will automatically put in the queue of specific tasks assigned to a specific thread and CPU (if it is a multi-core processors), specific how many threads created, assigned to which thread are made of systems management.

More to come

🌹 just like it 👍🌹

🌹 feel have harvest, can come a wave, collect + concern, comment + forward, lest you can’t find me next 😁🌹

🌹 welcome everyone to leave a message to exchange, criticize and correct, learn from each other 😁, improve self 🌹