preface

The definition and application of multithreading described in the previous article. This article focuses on exploring the most common way that multithreading is used in projects

The GCD definition

GCD: Adds the task to the queue and specifies the function to execute the task

Full name: Grand Central Dispatch, pure C language, provides many powerful functions

Advantage:

2.GCD will automatically use more CPU cores (such as dual-core, quad-core) 3.GCD will automatically manage the life cycle of threads (thread creation, task scheduling, thread destruction) 4. The user only needs to tell the GCD what task it wants to perform, without writing any thread management codeCopy the code

The function classification

1. Asynchronous dispatch_async-> execute the next statement without waiting for the current statement to complete

2. Synchronize dispatch_sync-> The next statement is executed only after the execution of the current statement is complete. The task of executing the current block is not enabled

The queue

1. Serial queues

Only one task can be executed at a timeCopy the code

2. Concurrent queues

Multiple tasks can be performed at onceCopy the code

Note: The current task is executed in a thread. Scheduling tasks through queues, relying on thread pools to only execute tasks, and all follow a FIFO, first-come-first-scheduler principle.

Functions and queues

1. Serial queue of synchronous function

The thread will not be opened, and tasks will be executed in the current thread. Tasks will be executed sequentially, which will cause congestion.Copy the code

2. Synchronize functions to parallel queues

Does not start the thread, in the current thread summary execution of tasks, one task after anotherCopy the code

3, asynchronous function serial queue

Start a new thread. One task after anotherCopy the code

4, asynchronous function parallel queue

Start the thread, in the current thread to execute the task, task asynchronous execution, no order, CPU scheduling related.Copy the code

Primary queue and global queue

Dispatch_get_main_queue () : a serial queue that dispatches tasks on the main thread but does not open a thread. If there are tasks running on the main thread, then whatever tasks are currently added to the main queue is not dispatched.

Global queue(dispatch_get_global_queue(0, 0)) global queue(dispatch_get_global_queue(0, 0)) global queue(dispatch_get_global_queue

Deadlock phenomenon

Definition: the main queue waits to execute its task first because of the synchronization function. The main queue waits for the main queue to complete its task before executing its own task. The main queue and the main queue waiting for each other cause a deadlock

GCD function dispatch_barrier

GCD barrier function dispatch_barrier: blocks the previous concurrent tasks and waits for the completion of the barrier function to execute subsequent concurrent tasks.

Warning: Fence functions cannot use global_queue

Difference between dispatch_barrier_async and dispatch_barrier_sync

The synchronous fence waits for tasks within the fence to complete before executing subsequent main thread or child thread tasks.

Asynchronous fencers do not wait for tasks in the fencers to complete before executing tasks on the main thread. The asynchronous fence function does not block the main thread.

The thread deadlock

Refers to two or more threads in the execution process, because of the contention for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to advance.