A simple introduction to multi-threading in iOS design

Source: Internet
Author: User

1. What is multi-threading, What are the performance patterns and what are the pros and cons ?

A process is a program's entity process, which is the execution of a program, the process is the thread of the container thread is a lightweight process, is the smallest unit of program execution flow

Multi-Threading concept: a single program that runs multiple threads at the same time to accomplish different tasks, called multithreading, is the technique of implementing multiple threads concurrently from software or hardware

Why use Multithreading: Save CPU time and avoid the application of multi-threading technology is the best way to prevent the main thread from clogging and increasing operation efficiency.

How to implement multi-threading (Presentation mode): Nsthread: Is lightweight in three ways, but needs to manage the life cycle of threads, synchronization, lock-up issues, will lead to a certain performance cost Nsoperationqueue: is based on OC implementation, in an object-oriented way to encapsulate the need to perform the operation, do not have to relate to thread synchronization, management issues Grand Central Dispatch (GCD): Provides some new features, runtime to support multi-core parallel programming, his focus is higher: how to improve efficiency on multiple CPUs

Advantages of Multithreading:

1.) Use multithreading to put the task of a long-occupied program in the background to perform

2.) The program may run faster

3.) In some waiting tasks to achieve user input, file read and write and network send and receive data, in this case, can release some precious resources such as memory consumption, etc.

Disadvantages of Multithreading:

1). If there are a large number of threads, it will affect performance because the operating system needs to switch between them

2). Typically block model data needs to be shared across multiple threads, requiring a thread deadlock condition to occur

Thread Overhead:

Running a thread requires memory resources and CPU, which creates memory and performance overhead. Memory overhead includes system kernel memory and application memory. This process is time-consuming for threads to interact with memory, which is the main reason for the performance overhead of threads. The thread overhead of creating a new thread is significantly less expensive than creating a new process, which is an important reason for our multithreaded development.

2. Thread Overhead

Approximate cost of thread creation:

1.) Kernel data structure: approximately 1KB

2.) stack space: The main thread is about 1M, the second thread is about 512k (the personal sense that the thread space does not decrement after the second thread, and still remains at 512k.) Assuming a decrease, the space that is occupied after the tenth thread is too small to accomplish almost any task.

3.) Thread creation time: approx. 90 ms

4.) Additional overhead is the overhead of program thread synchronization

3. Basic concepts in multi-threading

Parallel: Two lines of code executing simultaneously, here is not the same point in time execution, parallel only in the time of the task execution overlap concurrency: At the same time, two or more programs simultaneously execute, here refers to the macroscopic sense, there will always be errors, only multi-core processor to do the microscopic real simultaneous serial: in parallel with the Execute sequentially between several tasks. That is, there is no overlap on the execution task. Synchronous and Asynchronous: Synchronization refers to the relationship between two people, and one task needs to wait for the feedback from another task to continue. Asynchronous and synchronous Instead, one task does not need to wait for feedback from other tasks to continue executing.

4. Summary

In the process of learning multithreading, there are two points of ability to improve, one is the use of multi-threading tools, and the second is multi-threaded thinking. To be accustomed to using multi-threaded thinking to solve problems, but in the cultivation of multi-threaded programming ideas we need to use longer time to get used to and perfect.

A simple introduction to multi-threading in iOS design

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.