This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
preface
In the interview of iOS, multithreading is often asked, multithreading is also a difficult point, many interviewees usually do not use much, so it is difficult to answer to the point, so this blog will explore and analyze multithreading.
1. Processes and threads
What is a process
- A process is an application that is running on a system. It is an instance of the program when it is executed.
- When the program runs, the system creates a process, assigns resources to it, and then puts the process on the process ready queue. When the process scheduler selects it, it assigns CPU time to it, and the program actually runs.
- Each process is independent of each other, and each process runs in its own proprietary and protected memory space
- On a MAC, you can use Activity Monitor to see which processes are started
IOS development is single-process, Android is multi-process
What is a thread
- A thread is the basic execution unit of a process. All tasks in a process are executed in a thread
- Processes must have threads in order to execute tasks, and each process must have at least one thread
- The program starts with a thread by default, which is called the main thread or UI thread
The difference between processes and threads
- A process is the smallest unit of resource allocation and a thread is the smallest unit of program execution.
- Processes have their own separate address space, and every time a process is started, the system allocates address space for it, and sets up data tables to maintain code segments, stack segments, and data segments, which is very expensive. Threads share data in the process and use the same address space, so it is much cheaper for the CPU to switch a thread and much cheaper to create a thread than a process.
- The communication between threads is more convenient. Threads in the same process share global variables, static variables and other data, and the communication between processes needs to be carried out in the way of communication (IPC). However, how to deal with synchronization and mutex is the difficulty of writing multithreaded programs.
- However, multithreaded programs are more robust. If one thread dies, the entire process dies, and the death of one process does not affect the other process, because the process has its own address space.
2. The meaning of multithreading
The task of a process is multiple, single thread execution efficiency is certainly low, in the development of multithreaded programming, why use multithreading?
For example,
- The test code
NSLog(@ "start");
NSInteger count = 1000 * 100;
for (NSInteger i = 0; i < count; i++) {
/ / the stack area
NSInteger num = i;
/ / constant area
NSString *name = @"RENO";
/ / heap area
NSString *myName = [NSString stringWithFormat:@"%@ - %zd", name, num];
NSLog(@ "% @", myName);
}
NSLog(@ "end");
Copy the code
- The test results
2021- 08- 08 21:42:15.517924+0800 001---- The role of multithreading [35508:936771] start2021- 08- 08 21:42:15.518147+0800 001---- The role of multithreading [35508:936771] RENO - 0
2021- 08- 08 21:42:15.518314+0800 001---- The role of multithreading [35508:936771] RENO - 1
2021- 08- 08 21:42:15.518468+0800 001---- The role of multithreading [35508:936771] RENO - 2.021- 08- 08 21:43:03.151830+0800 001---- The role of multithreading [35508:936771] RENO - 99998
2021- 08- 08 21:43:03.152314+0800 001---- The role of multithreading [35508:936771] RENO - 99999
2021- 08- 08 21:43:03.152691+0800 001---- The role of multithreading [35508:936771End]Copy the code
In the above case, the loop executes 100,000 times, and local variables are created during the loop. This process takes nearly one minute to complete. If this process is placed on the main thread, the main thread will be stuck, which will greatly affect the user experience.
So in general, we do asynchronous processing and start new threads to process these transactions, but if a transaction is complex and time consuming, we can break a large transaction into several smaller transactions and process them concurrently, which saves time and does not affect the user experience.
Advantages and disadvantages of multithreading
advantages
:
- Can properly improve the efficiency of the implementation of procedures
- Can appropriately improve the utilization of resources (e.g
CPU, memory,
) - When the tasks on the thread finish executing, the thread is automatically destroyed
disadvantages
:
- Starting a thread requires a certain amount of memory space (by default, each thread uses this space
512
KB) - If a large number of threads are enabled, a large amount of memory space will be occupied and the performance of the program will be degraded
- The more threads,
CPU
The greater the overhead on the calling thread, the more complex the program design, such as communication between threads, multi-threaded data sharing
3. The time slice
A timeslice, also known as a “quantum” or “processor slice,” is a microscopic amount of CPU time (in a preemptive kernel: the time from the start of a process until it is preempted) allocated by a time-sharing operating system to each running process.
In simple terms, the CPU time slice is the time allocated by the CPU to multiple programs. Each thread is allocated a time segment, called its time slice.
- On a macro level, we can have multiple applications open at the same time, each running parallel;
- At the micro level, because there’s only one
CPU
How to deal with fairness? One way to deal with fairness is to introduce time slices for each programTake turns to perform
.
-
Multithreaded execution: The CPU quickly switches between multiple threads. If the number of threads is too large, the CPU will switch between multiple threads, destroying a large number of CPU resources and decreasing the execution efficiency.
-
Multithreading: A process in which the CPU switches between multiple threads quickly. The CPU schedules the threads quickly enough so that all threads are executing simultaneously.
-
If the number of threads is very large, the CPU will switch between N threads, which consumes a lot of CPU resources. As a result, the number of times each thread is scheduled is reduced, and the execution efficiency of the thread is reduced.
More to come
🌹 just like it 👍🌹
🌹 feel have harvest, can come a wave, collect + concern, comment + forward, lest you can’t find me next 😁🌹
🌹 welcome everyone to leave a message to exchange, criticize and correct, learn from each other 😁, improve self 🌹