IOS multithreading knowledge Summary/queue concept/GCD/serial/parallel/Synchronous/asynchronous, iosgcd

Source: Internet
Author: User

IOS multithreading knowledge Summary/queue concept/GCD/serial/parallel/Synchronous/asynchronous, iosgcd

Process: an ongoing program is called a process, which is responsible for the memory allocation of the program running. Each process has its own virtual memory space;

Thread: A thread is an independent execution path (control unit) in a process. A process contains at least one thread, that is, the main thread.

Queue: dispatch_queue_t, a type of first-in-first-out data structure. The creation and collection of threads do not require operations by programmers. The queue is responsible for this.

Serial queue: tasks in the queue are executed only sequentially (similar to running)

Dispatch_queue_t q = dispatch_queue_create ("...", dispatch_queue_serial );

Parallel queue: tasks in the queue are usually executed concurrently (similar to racing)

Dispatch_queue_t q = dispatch_queue_create ("...", dispatch_queue_concurrent );

Global queue: It is developed by the system and can be obtained directly. It is similar to the parallel queue, but the queue where the operation is located cannot be confirmed during debugging.

Dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

Main queue column: each application corresponds to a unique main queue column and can be directly obtained. In multi-threaded development, use the main queue column to update the UI.

Dispatch_queue_t q = dispatch_get_main_queue ();

Operation

Dispatch_async asynchronous operations will be executed concurrently, and the execution sequence of tasks cannot be determined;

The dispatch_sync synchronization operation is executed sequentially to determine the task execution sequence;

 

Serial queue synchronization: The operation will not create a thread or execute the operation in sequence;

Asynchronous Serial queue: An operation requires a sub-thread, which creates and recycles new threads and threads without the involvement of programmers. sequential execution of operations is the safest choice;

 

Parallel queue synchronization: The operation will not create a thread or execute the operation in sequence;

Asynchronous Parallel queue: The operation creates multiple threads (How many tasks are there and starts n threads for execution), and the operation is unordered. If there are other tasks before the queue, the task will be executed after the previous task is completed. Scenario: it does not affect the main thread and does not need to be executed.

Operations in sequence!

 

Asynchronous global queue: The operation creates multiple threads and unordered operations. If there are other tasks before the queue, the operation will be executed after the previous tasks are completed;

Global queue synchronization: The operation will not create a thread or execute the operation in sequence;

 

Asynchronous main queue columns: operations should be executed sequentially in the main thread, and there is no asynchronous operation;

Main queue column synchronization: If the operations in the main thread are regarded as a large block, the Operation will never end unless the main thread is killed by the user; synchronization operations added to the main queue columns will never be executed and will be deadlocked;

 

Results of dispathch_sync (synchronization) Tasks nested in different queues

// 1. Global queues are all executed on the main thread and will not deadlock dispatch_queue_priority_default

Dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

// 2. Parallel queues are all executed on the main thread and will not deadlock dispatch_queue_concurrent

Dispatch_queue_t q = dispatch_queue_create ("cn. itcast. gcddemo", dispatch_queue_concurrent );

// 3. The serial queue will be deadlocked, but will execute the code dispatch_queue_serial before the nested synchronization operation

Dispatch_queue_t q = dispatch_queue_create ("cn. itcast. gcddemo", dispatch_queue_serial );

// 4. deadlock in the main queue column: dispatch_get_main_queue ();

Dispatch_queue_t q = dispatch_get_main_queue ();

 

Dispatch_sync synchronous application development scenario

Blocking the execution of parallel queues requires that a certain operation be executed before subsequent operations, such as user login

Make sure that local variables other than block code are modified.

Dispatch_queue_t q = dispatch_queue_create ("cn. itcast. gcddemo", dispatch_queue_concurrent );

_ Block bool login = no;

Dispatch_sync (q, ^ {

Nslog (@ "simulate time-consuming operations % @", [nsthread currentthread]);

[Nsthread sleepfortimeinterval: 2.0f];

Nslog (@ "simulation time elapsed % @", [nsthread currentthread]);

Login = yes;

});

Dispatch_async (q, ^ {

Nslog (@ "Logon completed processing % @", [nsthread currentthread]);

});

Three multithreading technologies for ios:

1. nsthread

(1) It is very convenient to create a thread using the nsthread object

(2)! It is very difficult to use nsthread to manage multiple threads.

(3) tips! Use [nsthread currentthread] to track the thread where the task is located, applicable to these three technologies

2. nsoperation/nsoperationqueue

(1) A set of objective-c APIs implemented using gcd

(2) Object-oriented Thread Technology

(3) provides features that are not easily implemented in gcd, such as limiting the maximum number of concurrent operations and dependency between operations.

3. gcd -- grand central dispatch

(1) It is a C language-based underlying api

(2) defining tasks with blocks is flexible and convenient to use.

(3) provides more control capabilities and underlying functions not available in the Operation queue

 

1 ---- difference between queue and thread:

Queue: it is used to manage threads. It is equivalent to a thread pool and can manage when the thread is executed.

Queues are divided into serial queues and parallel Queues:

Serial queue: the threads in the queue are executed in sequence (not simultaneously)

Parallel queue: the threads in the queue run concurrently. There may be a question: isn't the queue FIFO? If the subsequent tasks are finished, how can we get out. It should be emphasized that the task is completed and the queue is not necessarily determined. The queue is generated only when the previous task is finished.

2 ----- there are also differences between the queue created by the main thread queue and the queue created by gcd.

The queue created by the main thread queue and gcd is different. The priority of the queue created in gcd is not as high as that of the main queue. Therefore, the serial queue in gcd does not block the main thread if no nested tasks are enabled in the synchronization task. There is only one possibility of deadlock, that is, in the serial queue, nested enable tasks may lead to deadlocks.

If synchronization cannot be enabled in the main thread queue, the main thread is blocked. You can only enable asynchronous tasks. Enabling asynchronous tasks does not enable new threads, but reduces the priority of asynchronous tasks and calls them when the cpu is idle. A synchronization task will seize the resources of the main thread and cause a deadlock.

3 ----- thread: contains many tasks (synchronous and asynchronous)

Differences between synchronization and Asynchronization:

The synchronization task has a high priority and has a running sequence in the thread. New threads are not enabled.

The asynchronous task has a low priority and there is no sequence of execution in the thread, so the cpu is idle. New threads are not enabled in the main queue, and new threads are enabled in other queues.

4 ---- main thread queue note:

Enable asynchronous tasks in the main queue. The code in the code block is still executed in the main thread instead of opening a new thread. Why is the thread not blocked?

> When an asynchronous task is enabled in the main queue, although a new thread is not enabled, the asynchronous task is reduced to a higher priority. When idle, the asynchronous task is executed on the main thread.

Why does a thread block when a synchronization task is enabled in the main queue?

> Enable the synchronization task in the main queue. Because the main queue is a serial queue, the threads in the queue are ordered. Only after one thread is executed can the next thread be executed, there is always only one main thread in the main queue, and the main thread will not be executed, because it is infinite loop, unless the application development program is closed. Therefore, when a synchronization task is started in the main thread, the synchronization task will want to seize the resources to be executed, and the main thread task is always executing some operations and will not let it go. Both of them have a high priority and eventually lead to deadlocks and thread blocking.

Refer to: http://www.makaidong.com/bloke garden heat /32951.shtml

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.