Introduction to multithreading and Multithreading

Source: Internet
Author: User

Introduction to multithreading and Multithreading

The purpose of this blog is to share with you some simple understanding of multithreading and sort out your own knowledge. If anything is inappropriate, you are welcome to correct it.

 

First, we need to understand two concepts:

Process: A program runs at a time and has an independent memory address space (an iOS application has only one Process ).

Thread ):

1. A thread is the basic execution unit of a process and the smallest unit that can be scheduled by the operating system. A process can have multiple threads or at least one thread.

2. multiple threads share the address space of the process. Each thread has its own stack space and shares the heap space.

3. Multiple Threads can be executed synchronously to improve CPU resource utilization.

 

Advantages and disadvantages of multithreading:

Advantage: it can improve the CPU utilization, prevent the main thread from being blocked, and prevent the UI from being suspended. Here we will remind you that the program is shown to you, updates to the UI are processed in the main thread as much as possible, and the main thread only processes UI updates and display as much as possible. Other operations such as data processing are placed in the Child thread, in this way, the lag delay between the user and the application interface is avoided.

Disadvantages: Each thread has its own thread stack, which requires a certain amount of space; Thread Scheduling consumes CPU time; program design is more complex, inter-thread communication, data sharing, and data security must be considered.

 

IOS multithreading method:

1. C Language Interface: pthread.

2. NSThread

3. NSOperation

4. GCD

Only GCD is introduced here.

GCD (Grand Central Dispatch) is a technology for asynchronous task execution. It encapsulates the difficulty "thread management" in multi-threaded development. Developers only need to define tasks and submit them to the Dispatch Queue, which is automatically managed and scheduled by GCD.

GCD uses block to define tasks. You can submit tasks synchronously or asynchronously.

// Synchronously submit the task. The queue is the task queue dispatch_sync (queue, ^ {/* task */}; // asynchronously submit the task dispatch_async (queue, ^ {/* task */};

 

The dispath_sync method will wait until the task execution is complete before returning the result. Waiting means that the thread executing dispatch_sync is "paused" and cannot submit the task to the current thread (the thread that submits the task is easy to deadlock ).

The dispath_async method does not return if the task is completed.

 

Queue type:

GCD has two queue types:

Serial queue: wait until the previous task is completed. Tasks in the serial queue are executed according to the first-in-first-out (FIFO) rule, and the previous task is completed before the latter task can be processed, and only one thread is used to process the queue.

Parallel queue: do not wait until the previous task is completed. Parallel queues use multiple threads to process queues at the same time. The number of tasks executed simultaneously is related to the number of threads. The number of threads is related to the number of CPU cores and CPU load. For example, there is a task in the parallel queue: from 1 to 100

For (int I = 0; I <100; I ++) {/** <this method is used to obtain the global queue of the system, and the global queue is a parallel queue. */Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {NSLog (@ "I = % d", I );});}

 

It can be found that the process ID is always 59249, but there are multiple thread IDs, and the printed numbers are not in the standard order. This is because of asynchronous thread processing, each thread is competing to execute the task, and the CPU schedules a certain number of threads to execute the task according to the current status. After each thread finishes the task, it continues to execute the next task. PS: the queue arranges threads to complete the task. The thread helps us to process the task. The main queue has only one main thread, so that the main thread of the system does less tasks we have arranged, so as to ensure the smoothness of the UI, once again, the UI update must be placed on the main thread.

-------- Split the line, and then write -------

 

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.