iOS multithreading knowledge Summary/queue concept/gcd/Serial/parallel/synchronous/asynchronous

Source: Internet
Author: User
Tags gcd

Process : The program in progress is called a process, which is responsible for the memory allocation of the program running; Each process has its own independent virtual memory space;

Threads : A thread is a separate execution path (control unit) in a process, and at least one thread is included in a process, that is, the main course.

queue :dispatch_queue_t, a first-in-one-out data structure, thread creation and recycling does not require programmer action and is the responsibility of the queue.

  Serial Queue : Tasks in the queue are only executed sequentially (like running)

      dispatch_queue_t q = Dispatch_queue_create ("....", dispatch_queue_serial);

  Parallel Queues : Tasks in the queue are usually executed concurrently (like running)

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

Global Queue   : It is system development, it can be used directly (get), similar to parallel queue, but cannot confirm the queue of operation when debugging

dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0);

Home Row   : Each application corresponds to a single home row, direct get; in multithreaded development, use the master queue to update the UI

      dispatch_queue_t q = Dispatch_get_main_queue ();

Operation

Dispatch_async asynchronous operation, will be executed concurrently, unable to determine the order of execution of the task;

Dispatch_sync synchronous operation, sequential execution, can determine the order of execution of tasks;

Serial Queue Synchronization : Operation does not create new thread, operation sequence execution;

Serial Queue Async : The operation requires a child thread, the creation and recycling of new threads, threads are not required to participate in the implementation of the operation sequence, is the safest choice;

Parallel Queue Synchronization : Operation does not create new thread, operation sequence execution;

Parallel Queue Async : The operation creates a new number of threads (with a number of tasks, executes on n threads), executes out of order, and, if there are other tasks before the queue, waits for the previous task to complete before executing; scenario: does not affect the main thread and does not require

Sequence of actions performed!

Global Queue Async : The operation creates multiple threads, executes out of order, and, if there are other tasks before the queue, waits for the previous task to complete before executing;

Global Queue Synchronization : Operation does not create new thread, operation sequence execution;

Home Row Async : The operation should be executed sequentially on the main thread, and there is no asynchronous;

Home Row Synchronization : If the main thread in the operation as a large block, then unless the main thread is killed by the user, otherwise it will never end; the synchronization operations added in the main queue will never be executed and deadlock;

Results of nested Dispathch_sync (synchronous) tasks for different queues

1. Global queues, which are executed on the main thread, do not deadlock Dispatch_queue_priority_default

dispatch_queue_t q = dispatch_get_global_queue (dispatch_queue_priority_default, 0);

//2. Parallel queues, which are executed on the main thread, do not deadlock dispatch_queue_concurrent

dispatch_queue_t q = dispatch_queue_create ("Cn.itcast.gcddemo", dispatch_queue_concurrent);

//3. Serial queue, deadlock, but the code before the nested synchronization operation is executed Dispatch_queue_serial

dispatch_queue_t q = dispatch_queue_create ("Cn.itcast.gcddemo", dispatch_queue_serial);

//4. Home row, direct deadlock dispatch_get_main_queue ();

dispatch_queue_t q = dispatch_get_main_queue ();

Dispatch_sync Synchronous Application Development Scenarios

Block the execution of a parallel queue, requiring that an operation be performed before subsequent operations, such as user logon

Ensure that local variables outside the block code are actually modified

dispatch_queue_t q = dispatch_queue_create ("Cn.itcast.gcddemo", dispatch_queue_concurrent);

__block BOOL Logi n = no;

Dispatch_sync (q, ^{

NSLog (@ "Analog time-consuming Operation%@", [Nsthread CurrentThread]);

[Nsthread sleepfortimeinterval:2.0f];

NSLog (@ "Analog time-consuming completion%@", [Nsthread CurrentThread]);

login = yes;  

});

Dispatch_async (q, ^{

NSLog (@ "Login completed processing%@", [Nsthread CurrentThread]);

});

iOS three multithreading technology :

1.nsthread

(1) It is very convenient to build a thread using the Nsthread object

(2) but! It is very difficult to manage multiple threads using Nsthread, it is not recommended to use

(3) Tips! Use [Nsthread CurrentThread] to track task threads for these three technologies

2.nsoperation/nsoperationqueue

(1) is a set of OBJECTIVE-C APIs implemented using GCD

(2) is an object-oriented threading Technology

(3) provides some features that are not easy to implement in GCD, such as limiting the maximum number of concurrent and the dependencies between operations

3.gcd--grand Central Dispatch

(1) is a C-based underlying API

(2) Define the task with block, which is very flexible and convenient to use.

(3) provides additional control capabilities and underlying functions that are not available in the Operation queue

1----The difference between a queue and a thread:

Queue: is the management thread, equivalent to the thread pool, can manage when the thread executes.

Queues are divided into serial queues and parallel queues:

Serial queue: Threads in the queue are executed sequentially (not concurrently)

Parallel queues: Threads in a queue are executed concurrently, and there may be a question that the queue is not FIFO, and if the later tasks are finished, how to get out. It needs to be stressed here that the task is done, not necessarily out of the queue. The queue only occurs when the previous task has finished executing .

2-----The queue created by the main thread queue and GCD is also different.

The main thread queue and the queue created by GCD are different. The queue priority created in GCD does not have a home column height, so there is no nested task inside the serial queue in GCD that does not block the main thread, only one of which could lead to deadlocks, which is the serial queue, nested open tasks that could lead to deadlocks.

Synchronization cannot be turned on in the main thread queue, blocking the main thread. Only the asynchronous task can be turned on, and the asynchronous task will not open the new thread, just reduce the priority of the asynchronous task, let the CPU idle time to call. synchronous tasks, which preempt the main thread's resources, can cause deadlocks.

3-----Threads: There are a lot of tasks (synchronous, asynchronous)

The difference between synchronous and asynchronous:

The synchronization task has a high priority, there is a sequence of executions in the thread, and no new threads are opened.  

Asynchronous task priority is low, execution in thread is not sequential, see CPU idle. New threads are not opened in the primary queue, and other queues open new threads.

4----Main thread queue Note:  

In the main queue, the asynchronous task is turned on, but the code in the code block is still executed in the main thread instead of opening new threads. Why isn't the thread blocked?

> The Home column opens the asynchronous task, although it does not open a new thread, but it lowers the priority of the asynchronous task, and at the same time, executes the asynchronous task on the main thread.

Why do I block threads when the synchronization task is turned on in the main queue?

> The synchronization task is turned on in the main queue because the home column is a serial queue, the threads inside are sequential, the next thread executes after executing a thread, and the host column always has only one main thread, and the main thread is not executed because he is an infinite loop, unless the application developer is closed. Therefore, in the main thread to open a synchronization task, the synchronization task will want to preempt the execution of resources, and the main thread task has been performing certain operations, refused to let go. Two priority levels are high, resulting in deadlocks and blocking threads.

Reference: http://www.makaidong.com/Blog Park Hot/32951.shtml

iOS multithreading knowledge Summary/queue concept/gcd/Serial/parallel/synchronous/asynchronous

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.