Definition of threads and processes
  • A thread is the basic execution unit of a process. All tasks of a process are executed in a thread

In order for a process to execute a task, it must have threads. A process must have at least one thread. By default, when a program starts, it starts a thread, which is called the main thread or UI thread

A process is an application running on the system. Each process is independent, and each process runs in its own dedicated and protected memory space. Activity monitor allows you to view the processes started on the MAC system

The relationship between processes and threads

Address space: Threads of the same process share the address space of the same process, but the address space between processes is independent. Resource ownership: Threads in the same process share the resources of the process, such as memory, I/O, and CPU, but the resources between the processes are independent. 1: If a process crashes, it will not affect other processes in protected mode, but if a thread crashes, the entire process will die. So multiple processes are more robust than multiple threads. 2: The process switchover consumes a large number of resources and is highly efficient. So when it comes to frequent switching, threads are better than processes. Similarly, if concurrent operations are required simultaneously and share some variables, only threads can be used, but not processes 3: Execution process: Each independent process has an entry point for the program to run, a sequence of execution, and a program entry point. However, threads cannot be executed independently, and must be dependent on the application program, which provides multiple thread execution control. 4: Threads are the basic unit of processor scheduling, but processes are not. 5: The thread has no address space, the thread is contained in the process address space

The meaning of multithreading
advantages

1, can properly improve the execution efficiency of the program 2, can properly improve the utilization of resources (CPU, memory) 3, after the completion of the task on the thread, the thread will automatically destroy

disadvantages

Open the thread take up some memory space (by default, each 512 KB) if open a large number of threads, will take up a lot of memory space, degrade the performance of the application thread, the more the greater the CPU overhead in the calling thread The program design is more complex, such as the communication between threads, multi-threaded data sharing

Time slice

The CPU can quickly switch between multiple tasks. The time interval is the time slice (single-core CPU). The CPU can only process one thread at a time, that is, only one thread is executing at a time

Simultaneous execution of multiple threads:

2. The CPU schedules the threads fast enough to cause the effect of “simultaneous” execution

Application – If the number of threads is very high

1, THE CPU will switch between N threads, consuming a large number of CPU resources. 2, the number of times each thread is scheduled will be reduced, the execution efficiency of the thread is low

The mutex

2. The scope of the mutex lock should be as small as possible. The larger the scope of the mutex lock, the worse the efficiency

Mutex parameters

Note: The lock object must be accessible to all threads. If there is only one place in your code that needs to be locked, most use self to avoid creating a single lock object

Nonatomic Nonatomic properties

The default value is to ensure that only one thread can write at the same time (but all threads can write at the same time). Nonatomic: Non-thread safe, suitable for mobile devices with small memory

In the process of iOS development, it is recommended that all properties be declared as nonatomic to avoid multiple threads snatching the same block of resources. Try to hand over the business logic of locking and resource snatching to the server side to reduce the pressure on the mobile side

The relationship between thread and Runloop

A runloop corresponds to a core thread. The core is because runloops can be nested, but there is only one core. Their relationship is stored in a global dictionary

2. The runloop is used to manage the thread. When the runloop is enabled, the thread will go to sleep after executing a task, and it will wake up to execute the task

3. The runloop is created on the first fetching and destroyed at the end of the thread

4. For the main thread, the runloop is created by default as soon as the program starts. 5. For the child thread, the runloop is lazily loaded and created only when we use it